At what point is it better to write tests? In general, there are three approaches:
- Write tests after your code
- Write tests while you're writing your code
- Write tests before you write code
In this lesson, we'll figure out the features of each approach.
Testing after code
In some situations, there isn't much choice. For example, in system testing, tests should simulate user behavior and perform actions in the browser. We write these tests after the code. The programmer can choose from the options above in integration, unit, and other low-level tests.
Testing before code
The tests-after-code approach is one of the less useful ones. Let's figure out why this is so. The very process of writing code involves constantly running the code and checking that it works. In the simplest tasks, the program can run quite quickly:
# Its is easy to run and to check results
capitalize('hello') # Hello
Preparing data to check the code can take dozens of minutes in real-life code. Moreover, the results produced after testing the code may be quite complex, for example, many records in a database or a complex structure.
The code test runs turn into a whole adventure: