FastAPI practical development of online course learning system -- registered interface unit test

preface

We shared the actual combat last time FastAPI (82) practical development interface development of online course learning system -- course on and off the shelf This time, we share the unit test of the registered interface.

text

The previous analysis is related to development and framework based learning. This time, we begin to share unit testing. Our unit testing is not just for a single class, but for the methods of more than one interface. Because we do not encapsulate many independent methods, we can use a separate interface as the entry point for our test.

We can first look at the articles related to the registered interface and recall the code of the registered interface.

FastAPI (65) practical development interface development of online course learning system -- user registration interface development .

Let's analyze the following interfaces:

We have made a detailed description of the interface in the excuse document. We can also look at the code at that time in the actual code development. Then we can design our use cases and design the relevant cases of our use case requirements according to the interface document.

There are two roles: one is a teacher and the other is a student.

Limit on length: both password and user name have length requirements. The password store is md5 encrypted.

Students have student numbers and length requirements. Teachers have job numbers and length requirements, and they are pure numbers.

Gender is divided into men and women.

Age should be greater than 18 and less than 65. We can design our use cases based on these known.

Design idea of use case:

1. Initialize the environment

2. Initialization parameters

3. Test case

4. Parameter cleaning

5. Environmental restoration.

Let's take a look at our last case. Here I just show some main cases, the most problematic cases.

class FastApiTestUserCreate(unittest.TestCase):

    @classmethod
    def setUpClass(cls) -> None:
        '''
        Initialize test environment
        :return:
        '''
        cls.client = TestClient(app)
        cls.testurl = "/user/create"

    @classmethod
    def tearDownClass(cls) -> None:
        '''
        Restore test environment, testing url
        :return:
        '''
        cls.client = None
        cls.testurl = ''

    def setUp(self) -> None:
        '''
        Initialization parameters
        :return:
        '''
        self.parame = {"username": "liwanle1i",
                       "password": "123456",
                       "role": "student",
                       "age": 19,
                       'studentnum': 20332}

    def tearDown(self) -> None:
        '''Final cleanup parameters'''
        self.parame.clear()

    def test_create_user(self):
        '''Add a data repeatedly'''
        response = self.client.post(self.testurl, json=self.parame)
        code = response.status_code
        reslut = response.json()
        self.assertEqual(code, 200)
        self.assertEqual(reslut['message'], 'repeat of user name')
        self.assertEqual(reslut['code'], 100104)

    def test_create_user_new(self):
        '''Add a new student'''
        self.parame['username'] = str(uuid.uuid1())[:9]
        response = self.client.post(self.testurl, json=self.parame)
        code = response.status_code
        reslut = response.json()
        self.assertEqual(code, 200)
        self.assertEqual(reslut['code'], 200)
        self.assertEqual(reslut['message'], 'success')

    def test_create_user_new_tearcher(self):
        '''Add a new teacher'''
        self.parame['username'] = str(uuid.uuid1())[:9]
        self.parame['role'] = "teacher"
        self.parame['jobnum'] = 93928
        self.parame['sex'] = "female"
        response = self.client.post(self.testurl, json=self.parame)
        code = response.status_code
        reslut = response.json()
        self.assertEqual(code, 200)
        self.assertEqual(reslut['code'], 200)
        self.assertEqual(reslut['message'], 'success')

    def test_create_user_new_one(self):
        '''The test student did not fill in the student status number'''
        self.parame['username'] = str(uuid.uuid1())[:9]
        self.parame.pop("studentnum")
        response = self.client.post(self.testurl, json=self.parame)
        code = response.status_code
        reslut = response.json()
        self.assertEqual(code, 200)
        self.assertEqual(reslut['code'], 100102)
        self.assertEqual(reslut['message'], 'Identity and corresponding number do not match')

    def test_create_user_new_tearcher_one(self):
        '''
        The job number of the test teacher is not filled in
        '''
        self.parame['username'] = str(uuid.uuid1())[:9]
        self.parame['role'] = "teacher"
        self.parame['sex'] = "female"
        response = self.client.post(self.testurl, json=self.parame)
        code = response.status_code
        reslut = response.json()
        self.assertEqual(code, 200)
        self.assertEqual(reslut['code'], 100102)
        self.assertEqual(reslut['message'], 'Identity and corresponding number do not match')


if __name__ == "__main__":
    unittest.main()

I just listed a part of the case. Of course, I need to add a lot. But I covered the most important case scenes. Students and teachers should be registered with student number or job number. Duplicate data cannot be added repeatedly.

You can fill in the input parameters, and then carry out two groups and use cases according to the orthogonal method, which covers more scenarios.

Postscript

Find and solve problems. If you encounter problems, you can solve them slowly.

Welcome to Leizi's test development. We will continue to share more technical knowledge for you in the future

If you have any questions, you can leave a message or add me to wechat: 952943386.

2021, a cow turns to Qian Kun and a cow forces.

If you think this article is good, let's have a [share, like and watch] three links to let more people see it~

Posted on Tue, 23 Nov 2021 03:13:34 -0500 by jomofee