第9章ソフトウェア工学
Test-Driven Developmentテスト駆動開発
Many organizations use test-driven development (TDD), which is a process where developers write tests before they write the code. This might seem counterintuitive, but it offers several benefits:
英語のヒント
多くの組織では、テスト駆動開発(TDD)を用いている。これは、開発者がコードを書く前にテストを書くプロセスである。直感に反するように思えるかもしれないが、次のような利点がある。
• Clarifies requirements: Writing tests first forces developers to think critically about the problem they are trying to solve and the expected behavior of the code. For example, if you are to write code that finds real roots of a quadratic equation and you start with unit tests, you will probably identify three cases that must be solved separately: the case when the equation has two roots, the case when the equation has a single root, and the case when the equation has no root.
英語のヒント
• 要求の明確化:テストを先に書くと、開発者は、解こうとしている問題とコードに期待される振る舞いを、吟味しながら考えざるを得なくなる。たとえば、二次方程式の実数解を求めるコードを書くにあたって単体テストから始めると、別々に処理すべき三つの場合に気づくだろう。方程式に二つの解がある場合、解が一つだけある場合、解がない場合である。
• Facilitates early bug detection: Writing tests first helps identify bugs early in the development process, when they are easier and less expensive to fix.
• バグの早期発見を容易にする:先にテストを書くと、修正が容易で費用も少なくて済む開発プロセスの早い段階で、バグを見つけやすくなる。
• Results in improved design: The test-driven approach can help identify edge cases and scenarios that might be overlooked during traditional development, leading to a more robust design.
英語のヒント
• 設計の改善につながる:テスト駆動の手法は、従来の開発では見落とされかねない境界付近の特殊な場合やシナリオを見つける助けとなり、より堅牢な設計につながる。
• Results in improved code quality: The focus on writing clear, testable code leads to overall higher code quality.
• コード品質の改善につながる:明快でテスト可能なコードを書くことを重視するため、コード全体の品質が向上する。
As illustrated in Figure 9.20, in test-driven development, you write unit tests first. You then run the tests, which should fail because there is not a fully implemented function to test against. You will then run (and rerun) the tests as you develop the functions. Development continues until all of the unit tests pass.
英語のヒント
図9.20に示すように、テスト駆動開発では最初に単体テストを書く。次にテストを実行するが、この段階ではテストが失敗するはずである。テスト対象となる、完全に実装された関数がまだ存在しないからだ。その後、関数を開発しながらテストを実行し、何度も実行し直す。すべての単体テストに合格するまで開発を続ける。

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典
Figure 9.20 In test-driven development, the developer writes the test before writing the code. Thus, when the test is first run, it should fail. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図9.20 テスト駆動開発では、開発者はコードを書く前にテストを書く。そのため、最初にテストを実行したときには失敗するはずである。(帰属:Rice University、OpenStax著作権、CC BY 4.0ライセンス。)
Here’s a breakdown of the steps of the test-driven development process:
英語のヒント
テスト駆動開発のプロセスを手順ごとに分けると、次のようになる。
1. Write a test: The process starts with the developer or development team writing a unit test that defines the expected behavior of a specific function or code unit.
1. テストを書く:開発者または開発チームが、特定の関数やコードの単位に期待される振る舞いを定めた単体テストを書くところから始める。
2. Run the test (and see it fail): Run the test. Initially, the test will likely fail because the function it tests doesn’t exist yet or isn’t implemented completely.
2. テストを実行し、失敗することを確認する:テストを実行する。最初は、対象の関数がまだ存在しないか、実装が完了していないため、おそらく失敗する。
3. Write just enough code to make the test pass: Focus on writing the minimum amount of code necessary to make it pass the test.
3. テストに合格するのに必要なだけのコードを書く:テストへの合格に必要な最小限のコードを書くことに集中する。
4. Run the test (and see it pass): When the code passes the test, this ensures it is fulfilling the intended functionality defined in the test.
英語のヒント
4. テストを実行し、合格することを確認する:コードがテストに合格すれば、テストで定めた意図どおりの機能を果たしていると確かめられる。
5. Refactor (improve code structure): Once the test passes, take a step back and refactor the code to improve readability, maintainability, and overall code structure without changing functionality. The process of restructuring source code without changing its functionality is called refactoring. We typically use refactoring to make code more readable. This can be as simple as renaming a variable or as complex as breaking the function up and splitting it across modules. Once the refactoring is complete, the unit tests should be executed again to make sure the code continues to pass.
英語のヒント
5. リファクタリングする(コードの構造を改善する):テストに合格したら、いったん引いた視点で見直し、機能を変えずにコードをリファクタリングして、読みやすさ、保守性、コード全体の構造を改善する。機能を変えずにソースコードの構造を組み直すプロセスを、リファクタリングという。一般に、コードを読みやすくするためにリファクタリングを行う。変数の名前を変えるだけの簡単な場合もあれば、関数を分解して複数のモジュールに分ける複雑な場合もある。リファクタリングが完了したら、単体テストをもう一度実行し、引き続き合格することを確かめる必要がある。
6. Run tests again: After refactoring, run all tests again to verify the code continues to pass and ensure no unintended side effects were introduced during refactoring.
6. テストをもう一度実行する:リファクタリングの後で、すべてのテストをもう一度実行する。コードが引き続きテストに合格し、リファクタリング中に意図しない副作用が入り込んでいないことを確かめる。
Once you’re done with all the steps, start the cycle over: Repeat this cycle (write test, run test, write code, refactor, run test) for each unit of functionality you develop.
英語のヒント
すべての手順を終えたら、サイクルを最初から始める。開発する機能の単位ごとに、テストを書く、テストを実行する、コードを書く、リファクタリングする、テストを実行する、というサイクルを繰り返す。
System Testingシステムテスト
Whereas unit testing focuses on the various units or pieces within a software solution, system testing focuses on the complete and fully integrated software product. The overall goal of system testing is to make sure the complete software solution works on the whole as expected. This is generally a black box style of testing that focuses on making sure the software meets all the requirements that were determined and checks that any possible scenarios that could be applied to the software function play out as expected. System testing is generally done by testers rather than the end users of software, and it involves reviewing both functional and nonfunctional requirements.
英語のヒント
単体テストがソフトウェアによる仕組みの中のさまざまな単位や部品に重点を置くのに対し、システムテストは、完成し、完全に統合されたソフトウェア製品に重点を置く。全体的な目標は、完成したソフトウェアによる仕組みが、全体として期待どおりに動作すると確かめることである。一般にはブラックボックス型のテストであり、定めたすべての要求をソフトウェアが満たすと確かめることを重視する。また、ソフトウェアの機能に適用しうるあらゆるシナリオが、期待どおりの展開になるかを確認する。通常、システムテストはソフトウェアのエンドユーザーではなくテスターが行い、機能要求と非機能要求の両方を確認する。
System tests can be performed by testers, or they can be automated. The form of system testing where a person must run the software system, provide any input, and manually check all output is called manual testing. Repeating tests requires these efforts to be repeated by the tester. The form of testing where a program or code is executed that tests the functionality of a software system or some of its parts is called automated testing. It allows for repeating tests without requiring repeated effort on the part of the tester. An example of such a program is Selenium. Selenium is a suite of free tools that automates the testing of web applications. The easiest way to use Selenium is to have it capture the interaction between your browser and web application. The interaction is saved as a script. You can then edit the script, if needed, and replay it to check if the web application still works. Selenium also provides libraries for many popular programming languages, including Java, C#, and Python, that allow users to write programs that perform this testing.
英語のヒント
システムテストは、テスターが実施することも、自動化することもできる。人がソフトウェアシステムを動かし、必要な入力を与え、すべての出力を手作業で確認するシステムテストの形態を、手動テストという。テストを繰り返すには、テスターがこれらの作業を繰り返さなければならない。ソフトウェアシステム、またはその一部の機能をテストするプログラムやコードを実行する形態を、自動テストという。テスターが作業を何度も繰り返す必要なく、テストを反復できる。そのようなプログラムの一例がSeleniumである。Seleniumは、Webアプリケーションのテストを自動化する無料のツール群である。最も簡単な使い方は、ブラウザーとWebアプリケーションのやり取りをSeleniumに記録させることである。やり取りはスクリプトとして保存される。必要ならそのスクリプトを編集したうえで再生し、Webアプリケーションが引き続き動作するかを確認できる。Seleniumは、Java、C#、Pythonなど、多くの一般的なプログラミング言語用のライブラリも提供している。これらを使い、ユーザーはテストを実行するプログラムを書ける。