第9章ソフトウェア工学
Unit Testing単体テスト
A crucial role in software development, the process of unit testing involves testing individual units of code, such as methods and functions, and it is usually done by developers during the development of the software or when updates are made. Software developers write scripts or code that test the functionality of a specific piece of code. Unit tests are typically added to a regression test suite, so they can be run again after each change to the source code to verify that the change did not break any existing functionality. Imagine a function that calculates the area of a rectangle. A unit test would verify that the function returns the correct area for different width and height values.
英語のヒント
単体テストはソフトウェア開発で重要な役割を果たす。メソッドや関数など、コードの個々の単位をテストするプロセスであり、通常はソフトウェアの開発中や更新時に開発者が行う。ソフトウェア開発者は、コードの特定の部分の機能をテストするスクリプトやコードを書く。単体テストは通常、回帰テストの一式に追加する。そうすれば、ソースコードを変更するたびに再実行し、変更によって既存の機能が壊れていないと確かめられる。長方形の面積を計算する関数を想像してみよう。単体テストでは、幅と高さにさまざまな値を与え、その関数が正しい面積を返すことを検証する。
Unit testing offers several benefits:
単体テストには、いくつかの利点がある。
• Early bug detection: Unit tests help identify bugs early in the development process, when they are easier and less expensive to fix.
• バグの早期発見:単体テストは、修正が容易で費用も少なくて済む開発プロセスの早い段階で、バグを発見する助けとなる。
• Improved code quality: The process of writing unit tests encourages developers to write clean, modular, and well-documented code.
英語のヒント
• コード品質の向上:単体テストを書く過程で、開発者は明快で、モジュールに分かれ、説明の十分なコードを書くよう促される。
• Maintainability: Unit tests serve as living documentation, clarifying the intended behavior of code components and making future modifications easier.
英語のヒント
• 保守性:単体テストは実態に即した文書として機能し、コード部品に意図された振る舞いを明確にするとともに、将来の変更を容易にする。
CONCEPTS IN PRACTICE概念を実際に使う
Staying in Their Lane車線からはみ出さないために
In the automobile industry, most new cars now include a lane detection system (LDS). This system includes code that verifies video images that are received by the automobile. The software checks to see if the vehicle crosses one of the lines painted on the road. If so, it activates a warning system to let the driver know. Units test for such a system include the following:
英語のヒント
自動車産業では、現在の新車の大半に車線検知システム(LDS)が搭載されている。このシステムには、自動車が受け取る映像を確認するコードが含まれる。ソフトウェアは、車両が道路に描かれた線のいずれかを越えるかどうかを調べる。越えた場合は、警報システムを作動させて運転者に知らせる。このようなシステムの単体テストには、次のようなものがある。
• An appropriate line is recognized.
• 適切な線が認識される。
• A warning occurs if a line appears to be crossed.
• 線を越えたと見られる場合に、警報が出る。
• The warning ends after the designated period of time.
英語のヒント
• 指定された時間が経過すると、警報が止まる。
• When the LDS system is turned off, the warning is not triggered.
英語のヒント
• LDSシステムを無効にしているときには、警報が作動しない。
Software engineers as well as testers perform these tests when the code for the LDS system is created as well as any time any updates are made to the system.
英語のヒント
テスターだけでなくソフトウェアエンジニアも、LDSシステムのコードを作成したときや、システムを更新するたびに、こうしたテストを行う。
Code Coverage
コードカバレッジ
Proper unit testing improves the quality of a software system and helps discover bugs early. In simple terms, a software bug is an issue or error with software programming. To find bugs, it is important to review and run the code.
適切な単体テストは、ソフトウェアシステムの品質を高め、バグを早期に見つける助けとなる。簡単に言えば、ソフトウェアのバグとは、ソフトウェアのプログラミングにおける問題や誤りである。バグを見つけるには、コードを確認し、実行することが重要である。
The range of a unit test is typically measured in terms of code coverage, which is the measurement of the percentage of code that is activated or reviewed by the test. Common types of code coverage are statement coverage, line coverage, and path coverage. Measurement of the percentage of statements that are activated at least once when you run all tests is statement coverage. Measurement of the percentage of activated lines of source code that are tested is line coverage. Measurement of the percentage of paths through source code that you go through is path coverage. As shown in Figure 9.18, with path coverage, if you have two conditional statements, one after the other, there are typically four paths through this code:
英語のヒント
単体テストが対象とする範囲は、通常、コードカバレッジで測る。これは、テストによって実行または確認されたコードの割合を測定したものである。一般的なコードカバレッジには、命令網羅率、行網羅率、経路網羅率がある。すべてのテストを実行したときに、少なくとも一度は実行される文の割合を測るのが、命令網羅率である。テスト対象となるソースコードの行のうち、実行された行の割合を測るのが、行網羅率である。ソースコードに存在する経路のうち、実際に通った経路の割合を測るのが、経路網羅率である。図9.18に示すように、条件文が二つ続けて現れる場合、経路網羅率で考える経路は通常、次の四つになる。
1. Both conditions are true.
1. 両方の条件が真である。
2. The first condition is true and the second is false.
2. 第一の条件が真で、第二の条件が偽である。
3. The first condition is false and the second is true.
3. 第一の条件が偽で、第二の条件が真である。
4. Both conditions are false.
4. 両方の条件が偽である。

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典
Figure 9.18 This activity diagram illustrates how path coverage works with four possible paths: (i) x and y positive, (ii) x positive, y negative or zero, (iii) x negative or zero, y positive, and (iv) x and y negative or zero. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図9.18 このアクティビティ図は、経路網羅率の考え方を、四つの可能な経路で示している。(i) xとyが正、(ii) xが正でyが負またはゼロ、(iii) xが負またはゼロでyが正、(iv) xとyが負またはゼロである。(帰属:Rice University、OpenStax著作権、CC BY 4.0ライセンス。)
Path coverage is more demanding and typically requires more tests than statement or line coverage, but tests are better. You need a minimum of four tests to achieve 100% path coverage, but a minimum of two tests to achieve the same level of statement coverage.
英語のヒント
経路網羅率を満たすための条件はより厳しく、通常は命令網羅率や行網羅率より多くのテストを必要とするが、テストの質は高くなる。経路網羅率を100%にするには最低四つのテストが必要だが、命令網羅率を同じ水準にするには最低二つのテストでよい。
Support for Unit Testing
単体テストの支援
Unit testing is so common that major programming languages provide some support for automated unit testing. An example is JUnit, a unit testing framework for the Java programming language, which is shown in Figure 9.19. JUnit facilitates the writing and managing of tests, and it can be run in some development environments quickly, typically via a single click. Many of the other major programming languages have similar frameworks or tools. There are also third-party software products that can be used to help with unit testing.
英語のヒント
単体テストは非常に広く行われるため、主要なプログラミング言語では、単体テストの自動化を何らかの形で支援している。一例が、図9.19に示すJavaプログラミング言語用の単体テストの枠組み、JUnitである。JUnitはテストの作成と管理を容易にし、一部の開発環境では、通常は一度クリックするだけですばやく実行できる。ほかの主要なプログラミング言語の多くにも、同様の枠組みやツールがある。単体テストを支援する、第三者が提供するソフトウェア製品もある。

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license; credit: NetBeans by Sun Microsystems · CC BY 4.0 · Source出典
Figure 9.19 JUnit test window in NetBeans integrated development environment (IDE). You can run all tests by clicking on the green arrows. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license; credit: NetBeans by Sun Microsystems)
図9.19 NetBeans統合開発環境(IDE)内のJUnitテスト画面。緑色の矢印をクリックすると、すべてのテストを実行できる。(帰属:Rice University、OpenStax著作権、CC BY 4.0ライセンス。クレジット:Sun MicrosystemsによるNetBeans。)