HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <p>At what point is it better to write tests? In general, there are three approaches:</p>
1 <p>At what point is it better to write tests? In general, there are three approaches:</p>
2 <ul><li>Write tests after your code</li>
2 <ul><li>Write tests after your code</li>
3 <li>Write tests while you're writing your code</li>
3 <li>Write tests while you're writing your code</li>
4 <li>Write tests before you write code</li>
4 <li>Write tests before you write code</li>
5 </ul><p>In this lesson, we'll figure out the features of each approach.</p>
5 </ul><p>In this lesson, we'll figure out the features of each approach.</p>
6 <h2>Testing after code</h2>
6 <h2>Testing after code</h2>
7 <p>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.</p>
7 <p>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.</p>
8 <h2>Testing before code</h2>
8 <h2>Testing before code</h2>
9 <p>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:</p>
9 <p>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:</p>
10 <p>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.</p>
10 <p>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.</p>
11 <p>The code test runs turn into a whole adventure:</p>
11 <p>The code test runs turn into a whole adventure:</p>
12 <p>It is where writing tests before code comes into play. For many novice developers, this phrase causes a bit of a stupor. How can we write tests before the code? It turns out you can.</p>
12 <p>It is where writing tests before code comes into play. For many novice developers, this phrase causes a bit of a stupor. How can we write tests before the code? It turns out you can.</p>
13 <p>Suppose we want to write a function that repeats a string passed to it a specified number of times:</p>
13 <p>Suppose we want to write a function that repeats a string passed to it a specified number of times:</p>
14 <p>We know two important things:</p>
14 <p>We know two important things:</p>
15 <ul><li>What is a<a>pure function</a></li>
15 <ul><li>What is a<a>pure function</a></li>
16 <li>What does this function take as input</li>
16 <li>What does this function take as input</li>
17 </ul><p>Knowing this means we can already write tests:</p>
17 </ul><p>Knowing this means we can already write tests:</p>
18 <p>An experienced developer can write this code in 15-20 seconds. But to check that this code runs, we need to type poetry run pytest in the console.</p>
18 <p>An experienced developer can write this code in 15-20 seconds. But to check that this code runs, we need to type poetry run pytest in the console.</p>
19 <p>Testing before writing code has another advantage - it forces the programmer to think not about the code but about the design of their solution. It pushes the programmer to think about how people will use their application. It is a way to create competent interfaces and is often the key to success.</p>
19 <p>Testing before writing code has another advantage - it forces the programmer to think not about the code but about the design of their solution. It pushes the programmer to think about how people will use their application. It is a way to create competent interfaces and is often the key to success.</p>
20 <h2>Testing during code</h2>
20 <h2>Testing during code</h2>
21 <p>In the development world, there is an approach in which we write tests before the code. It's called<strong>Test-Driven Development</strong>:</p>
21 <p>In the development world, there is an approach in which we write tests before the code. It's called<strong>Test-Driven Development</strong>:</p>
22 <p>According to the inventors, development through testing implies that the entire development process consists of a repeating cycle:</p>
22 <p>According to the inventors, development through testing implies that the entire development process consists of a repeating cycle:</p>
23 <ul><li>Programmers write a test for each iteration</li>
23 <ul><li>Programmers write a test for each iteration</li>
24 <li>They see that the test doesn't pass</li>
24 <li>They see that the test doesn't pass</li>
25 <li>They add code that satisfies this test</li>
25 <li>They add code that satisfies this test</li>
26 </ul><p>Rinse and repeat. As a result, we build the application step by step.</p>
26 </ul><p>Rinse and repeat. As a result, we build the application step by step.</p>
27 <p>Thanks to it gaining traction, everyone is talking about this method. In it, we write tests for all parts of the code with maximum detail. This kind of TDD aims towards the importance of design but focuses on specific functions and classes in the application instead of the whole picture.</p>
27 <p>Thanks to it gaining traction, everyone is talking about this method. In it, we write tests for all parts of the code with maximum detail. This kind of TDD aims towards the importance of design but focuses on specific functions and classes in the application instead of the whole picture.</p>
28 <p>But there is another approach to TDD, where developers rarely write tests for internal parts. You can read more about this in the article in the additional materials.</p>
28 <p>But there is another approach to TDD, where developers rarely write tests for internal parts. You can read more about this in the article in the additional materials.</p>