Writing automated unit tests is encouraged because of the trend of Agile and TDD movements. However, it is important to write good tests instead of just writing tests.
Other resources:
https://blog.cleancoder.com/uncle-bob/2014/12/17/TheCyclesOfTDD.html
https://www.ibm.com/garage/method/practices/code/practice_test_driven_development/
The tests and the production code are written together, with the tests just a few seconds ahead of the production code.
First Law
You may not write production code until you have written a failing unit test.
Second Law
You may not write more of a unit test than is sufficient to fail, and not compiling is failing.
Third Law
You may not write more production code than is sufficient to pass the currently failing test.
Phase 1
Write a first failing unit test:
Phase 2
Since the unit test testGetNumbersFromStringKeyWithSpaces
will fail, should modify the production codes to pass the test case, instead of creating more test cases:
Phase 3
Can start to write another production method after all unit tests pass:
When tests are dirty, it's almost the same as no tests. Discipline is:
Test code is just as important as production code.
Keeping unit tests clean helps keep production code flexible and makes changing it easier and more confident.
The clean test is all about readability. Readability includes:
Use the BUILD-OPERATE-CHECK pattern
http://fitnesse.org/FitNesse.FullReferenceGuide.UserGuide.WritingAcceptanceTests.AcceptanceTestPatterns.BuildOperateCheck
Write your own API for test use, and you need to constantly update and refactor according to test needs.
Before having APIs:
After writing and using the APIs:
While APIs for testing needs to be as simple and clean as production code, they don't need to be as efficient as production code. The reason is that the tests are run in a test environment, which has different requirements than the production environment.
Need to move eyes back and forth to read the test:
Increase the readability:
How the getState
method is implemented:
Sometimes the same code can appear in multiple tests when there is only one assert in each test:
To solve the above duplication of code problem, can use the Template Method.
Source https://www.artima.com/weblogs/viewpost.jsp?thread=35578
Use Given, When, Then:
Example in pseudocodes:
Test a single concept in each test function. When there are multiple concepts in a test:
The scenarios and preconditions in the above test:
Given the last day of a month with 31 days (like May):
Scenario 1:
Add 1 month, the last day of the new month should be 30th
Scenario 2:
Add 2 months, the last day of the new month should be 31st
Given the last day of a month with 30 days in it (like June):
Scenario 3:
Add 1 month, the last day of the new month should be 30th
The above test should be divided into 3 tests. Should also include other missing tests like the last day of a month with 28 days in it (like February)
Tests should be fast. They should run quickly.
Tests should not depend on each other.
Tests should be repeatable in any environment.
For example, tests should be able to run on both the production environment and the OA environment.
The tests should have a boolean output. Either they pass or fail.
The tests need to be written in a timely fashion. Unit tests should be written just before the production code that makes them pass.
We should always keep our tests clean.
learn
clean code
test automation