automated test that - Verifies a small piece of code (also known as a unit)*, - Does it quickly,* - Does it in an isolated manner.* - And usually written by the programmers themselves using their regular tools. * Vladimir Khorikov, “Unit Testing Principles, Practices, and Patterns”, Manning, January 2020
automated test that - Verifies a small piece of code (also known as a unit), - Does it quickly, - Does it in an isolated manner. - And usually written by the programmers themselves using their regular tools NOTICE: “unit” is changed by context. Object-Oriented Programming may treat a class as the unit. But, Functional Programming may treat a single function as a unit.
should follow the AAA (Act, Arrange, Assert) pattern. - Arrange: Set up objects to be tested. (If needed, mock some objects.) - Act: Execute the test code. (usually one line code) - Assert: Verifies that the executed code behaves as expected.
should follow the AAA (Act, Arrange, Assert) pattern. - Arrange: Set up objects to be tested. (If needed, mock some objects.) - Act: Execute the test code. (usually one line code) - Assert: Verifies that the executed code behaves as expected. Naturally, we start writing the test with the Arrange Section. But, if you like TDD, you may start writing the test with the Assert Section.
growth of the software project. - to decrease cost of correction through early detection of defects - to decrease debugging cost - to decrease maintenance cost by design improvement
growth of the software project. - to decrease cost of correction through early detection of defects - to decrease debugging cost - to decrease maintenance cost by design improvement One of the elements of sustainable growth is cost savings.
testing needs to be: 1. using coverage metrics 2. readable tests 3. fast enough to execution 4. keeping Isolation from external dependencies Keep it mind, these are just a part of better unit testing!
whether we test enough or not. How: - A lot of test libraries have coverage report feature, so let’s use it. Notice: - Coverage metrics can’t possibly tell whether your tests are exhaustive; nor can they say if you have enough tests. - The best way to view a coverage metrics is as an indicator, not a goal in and of itself.
maintain unit tests. How: - Naming (test case name & variables), Using fixtures, following AAA-pattern, parameterized tests Notice: - It is important to treat the test code with the same care as the production code.
maintain unit tests. How: - Naming (test case name & variables), Using fixtures, following AAA-pattern, parameterized tests Notice: - It is important to treat the test code with the same care as the production code.
Don’t follow a rigid naming policy. - Name the test as if you were describing the scenario to a non-programmer who is familiar with the problem domain. (= domain expert) - Separate words with underscores. 😰: `public void Delivery_with_invalid_date_should_be_considered_invalid()` 🥰: `public void Delivery_with_a_past_date_is_invalid()` - Later one is readable and understandable.
execution speed makes unit tests easier to run. - Ease of execution leads to motivation and productivity to write unit tests. How: - Solve the bottleneck of slow execution speed and refactor test codes. (e.g. external I/O) - Use a test library with fast execution speed. (for JSer, @swc/jest is hot lately) - Only focus on logic that completes on memory. Notice: - “if you can't measure it, you can't manage it.” - Speed is important for productivity in team. Especially, for TDD fans.
tests cannot be executed at any time and in any order, they will be unreliable. How: - Use Test Doubles properly (This is hard things maybe). - Single-Responsibility is also effective for unit tests. Notice: - In the isolation issue is hot topic in unit tests. - Test Doubles and Two schools (London and Detroit).