Slide 1

Slide 1 text

Better Unit Testing @unvalley_ Background Image: https://unsplash.com/photos/VUm2CFyUTXI

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Agenda 1. What is a unit test? 2. The goal of unit test 3. How to better unit testing?

Slide 4

Slide 4 text

@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

Slide 5

Slide 5 text

Agenda 1. What is a unit test? 2. The goal of unit test 3. How to better unit testing?

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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.

Slide 8

Slide 8 text

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.

Slide 9

Slide 9 text

Arrange

Slide 10

Slide 10 text

Act

Slide 11

Slide 11 text

Assert

Slide 12

Slide 12 text

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.

Slide 13

Slide 13 text

Agenda 1. What is a unit test? 2. The goal of unit test 3. How to better unit testing?

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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.

Slide 16

Slide 16 text

Agenda 1. What is a unit test? 2. The goal of unit test 3. How to better unit testing?

Slide 17

Slide 17 text

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!

Slide 18

Slide 18 text

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.

Slide 19

Slide 19 text

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.

Slide 20

Slide 20 text

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.

Slide 21

Slide 21 text

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.

Slide 22

Slide 22 text

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.

Slide 23

Slide 23 text

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.

Slide 24

Slide 24 text

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).

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Better Unit Testing @unvalley_ Background Image: https://unsplash.com/photos/VUm2CFyUTXI