Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

Better Unit Testing

unvalley
February 11, 2022

Better Unit Testing

Presentation slides for English Night #13 (https://gaogao.connpass.com/event/224068/).

unvalley

February 11, 2022
Tweet

More Decks by unvalley

Other Decks in Programming

Transcript

  1. This talk provides 1. Opportunity to reflect unit testing 2.

    Tips to write valuable unit test 3. Resources to dive into unit test more deeply
  2. Agenda 1. What is a unit test? 2. The goal

    of unit test 3. How to better unit testing?
  3. @unvalley_ Software Engineer ?? Student (M1) Write: TypeScript (Node.js, React.js)

    / Scala a little Interest: Software Design / Testing / NLP Twitter: twitter.com/unvalley_ GitHub: github.com/unvalley
  4. Agenda 1. What is a unit test? 2. The goal

    of unit test 3. How to better unit testing?
  5. What is a unit test? A unit test is an

    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
  6. What is a unit test? A unit test is an

    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.
  7. The structure of a unit test(AAA Pattern) All unit tests

    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.
  8. Act

  9. The structure of a unit test(AAA Pattern) All unit tests

    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.
  10. Agenda 1. What is a unit test? 2. The goal

    of unit test 3. How to better unit testing?
  11. The goal of unit test is = to enable sustainable

    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
  12. The goal of unit test is = to enable sustainable

    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.
  13. Agenda 1. What is a unit test? 2. The goal

    of unit test 3. How to better unit testing?
  14. How to better unit testing? (in my view) Better unit

    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!
  15. 1. Using coverage metrics Why: - Coverage metrics shows us

    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.
  16. Example of coverage metrics by Jest It is very easy

    to see the coverage metrics in Jest! `jest --collect-coverage` command output index.html like the shown above in the “/coverage” directory.
  17. 2. Readable tests Why: - Readable tests help developers to

    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.
  18. 2. Readable tests Why: - Readable tests help developers to

    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.
  19. Naming Guidelines from “Unit Testing Principles, Practices, and Patterns” -

    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.
  20. 3. Fast enough to execution (= Speed) Why: - Faster

    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.
  21. 4. Keeping Isolation from external dependencies Why: - If unit

    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).
  22. References - Vladimir Khorikov, “Unit Testing Principles, Practices, and Patterns”

    - Kent Beck, “Test Driven Development: By Example” - Kazuki Higashiguchi, “ユニットテストの現場の問題を原則に立ち返って考える”, https://speakerdeck.com/hgsgtk/think-deep-unit-test-practical-problem