Take the Django project as an example, this article quickly teaches you to build unit tests from scratch!

Preface

In the test pyramid, unit tests are at the bottom of the pyramid and account for the largest proportion. It runs fast and finds and locates problems faster. And once a unit test with sufficient coverage is established, it will greatly increase confidence in the release.

step

Regardless of the unit, interface or UI, the test steps are generally divided into the following three steps:

Test data preparation: Create users or courses that need to meet certain conditions, and do some processing after completion to make them meet the scenarios you need to test.

For example, if you need to test the refund function, you must first create data about users, courses, and courses purchased by users.

Call the method that needs to be tested.

Assertion: Get the result returned in step 2 and judge the result of the test.

data preparation

In Django projects, it is recommended to use FactoryBoy, which can create data based on your models in Django. The sample code is as follows:

Insert picture description here
It creates data based on the model you gave him. The fields that are not set with default values ​​in the model need to be assigned in the code. Calling UserFactory() or UserFactory.create() in your test code will create a user named john in the database. Of course, you may not want to create a user with the same name every time. So you can modify the code as follows:
Insert picture description here
so that every time UserFactory is called, the username will be incremented, from robot1 to robotN. The data that needs to be created often has foreign keys. Assuming a test scenario where a user signs up for an exam, this situation generally contains three tables, a user table, an exam table, and a registration record form. Store user registration information, in which exams and users are foreign keys. Correspondingly, we need to create three factories when testing: UserFactory, ExamFactory and EnrollFactory. The SubFactory code needed to create the EnrollFactory is roughly as follows: In
Insert picture description here
this way, each time UserFactory is called, the username will be incremented, from robot1 to robotN. The data that needs to be created often has foreign keys. Assuming a test scenario where a user signs up for an exam, this situation generally contains three tables, a user table, an exam table, and a registration record form. Store user registration information, in which exams and users are foreign keys. Correspondingly, we need to create three factories when testing: UserFactory, ExamFactory and EnrollFactory. The SubFactory code that needs to be used when creating EnrollFactory is roughly as follows:
Insert picture description here
So when EnrollFactory is called, it will first create UserFactory and ExamFactory.

Method call

The test focus of unit testing is often whether a certain method is correct, regardless of the interaction of each module, usually mock the way to simulate the return of other modules. Or sometimes we need to deliberately create a certain kind of exception, and we also need to use the mock method. You can usually use return_value, side_effect, etc., the following is the pseudo code of some common methods:
Insert picture description here
assertion

The TestCase object of unittest has many assert methods that can be used, the commonly used ones are assertTrue, assertFalse, assertEqual and assertRaises. The following is a pseudo code example:
Insert picture description here
sample

A common unit test is as follows:
Insert picture description here
coverage

After writing some unit tests, you often need to pay attention to coverage. Each language has some packages for statistical coverage. Coverage can be used in Django. When there is no coverage report, we run the test in django to execute:

Insert picture description here

If you need to test the coverage, then execute: In
Insert picture description here
this way, in the process of running the test, a .coverage file will be generated in the current directory to record the coverage information. Of course, you can’t read this file directly. You also need a command to generate xml or html format. Coverage report.
Insert picture description here
After execution, the coverage.xml file is generated in the current folder, which can be used for subsequent display.

to sum up

This article mainly summarizes the general steps when doing unit testing, and at the same time briefly introduces some packages that need to be used for unit testing in Django projects. I hope it will be helpful to students who are just starting to do unit testing.

Three. Concluding remarks

No winter will not pass, no spring will not come. The past 2020 was an extraordinary year for people all over the world. Everyone is fighting the epidemic bravely and bravely. Here we encourage ourselves together. Palm it, 2021 has come as promised, set a good goal and continue to grow up.

Here I recommend a software testing exchange group I created by myself, qq:642830685. The group will share software testing resources, test interview questions and industry information from time to time. You can actively exchange technology in the group.

May you and I meet and you will find something! Welcome to follow the WeChat public account: Yuan Yifei, the following hard-core resources are yours.

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_54696666/article/details/113956570