Source: Software Testing course at ITMO university by Kochubeev Nikolay Sergeevich ====== Unit tests ====== Unit tests check individual units of software (be it functions, methods or classes) in isolation from the rest of the system. The goal is to ensure that every module works as intended. They're usually placed in a separate from the source code directory. ==== Concepts ==== [[test_doubles|Test doubles]] are used to ensure Unit tests are truly isolated. [[testing_factory|Factories]], [[testing_builders|builders]] and [[random-based_tests|random-based tests]] are used to generate test data. [[property-based_testing|Property-based testing]] is an approach to testing in which we test the properties of a function rather than individual examples. [[data-driven_testing|Data-driven testing]] is an approach in which one test function is executed multiple times with different sets of input data. [[fixture|Fixture]] is a defined and reliable context for the tests. ==== Benefits ====== - Units tests ensure quick feedback. - They give confidence in refactoring. - They document code's behavior. - They can help find errors at the early stages. ==== Principles of good unit tests ==== * **[[first|FIRST]]:** Fast, Isolated, Repeatable, Self-validating, Timely; * **[[arrange_act_assert|AAA]]:** Arrange, Act, Assert; * **[[given_when_then|GWT]]:** Given-When-Then. ==== Pros and Cons ==== **Pros:** * quick feedback; * behavior documentation; * fast completion. **Cons:** * do not test module interaction; * require support with changes to API; * do not find integration errors. ==== Popular testing frameworks ==== - **JUnit** (Java) -- standard in the ecosystem, annotations, parametrization, Maven/Gradle and IDE integration. - **NUnit** (C#) -- flexible attributes ([TEST], [TestCase]), rich assert set. - **xUnit** (.NET Core) -- [Fact]/[Theory], modern style, .NET CLI integration. - **Go Testing** (Go) -- built-in, minimalistic, table-driven tests. - **pytest** (Python) -- fixtures, marking, parametrization, simplicity.