1. Unit testing with pytest
Now, we will learn about unit tests.
2. What is a unit test
Units in unit tests are the smallest working parts that can be tested. Usually, units are represented as functions or methods, but sometimes as modules or classes. A unit test is a test that verifies that a software unit works as expected. That means we can ensure that the smallest tiny piece of code actually works.
We also need to define a test case for this video. A test case is a set of unit inputs and expected outputs summarizing a particular piece of problem that the unit can solve. That is very important because a test case is actually a blueprint for the future test.
3. Why to use unit tests
That is the power that unit tests give us: if the smallest isolated parts work, then we can test the bigger picture. Thus, unit tests are also a foundation for other testing types.
We are using unit tests often. For example, when we find a bug and want to fix it but need help finding where to look for it. Or we can create unit tests during the whole development process to ensure that the function returns exactly what it should.
It is also important to run the tests after we push changes to the project. Any change can cause new, unexpected problems and bugs, so we need to verify that this is not the case.
4. How to create a unit test
Let's assume we have some software to test, and we want to do that with unit tests. What should we start?
First, we should choose the units that we want to test.
Second, we are going to define the test cases. It is the most creative and important part. To do that, we should think by asking ourselves questions about the unit: "What are the possible unit outcomes?", "How can one use the unit?", "How should the unit behave in all those cases?". After that, we can write down your thoughts and group them - that is the core of our future tests - the test cases.
Then, we can easily implement code for each test case.
Finally, we can easily write code for each test case.
5. Creating a unit test: example
Let's create some tests in pytest. Assume we have a function that, for an array, returns a sum of its elements. That is our unit. Next, we want to generate some test cases and implement test code for them.
What could be the test cases for that unit? We should start by ensuring the unit works when the input is a list of numbers as expected. Then we should consider some non-trivial cases, like if the input is an empty list or a list containing one number.
6. Creating a unit test: code
For the first test case, our pytest code would include a couple of lists with several integer numbers. For the second test case, it would include an empty list. And for the third one - a list with only one number. In real project, there might be more test cases, like checking that the function works for unexpected inputs. For example, that the function works if input is not a list, or when its elements are not integers.
7. Summary
We learned about the foundation of the testing - unit tests. Unit testing aims at verifying that a unit works as expected. A test case is a set of unit inputs and expected outputs summarizing a particular piece of the problem. We use unit tests during development, after changes, and to fix found bugs. It is crucial to define test cases before implementing the test code.
8. Let's practice!
You know enough to write your own unit tests. Try it!