当前位置:网站首页>Pytest (3) - Test naming rules

Pytest (3) - Test naming rules

2022-07-06 20:23:00 Here is a blank sheet of paper for you

Preface

In the automation test project , When the unit test framework is running, you need to search the test module first ( That is, where the test case is located .py file ), Then search the test class or test function in the test module , Then search the test method in the test class , Finally, join the queue , Then execute the test in the order of execution .

therefore , Only test module 、 Test class / Test functions 、 The test methods conform to the naming rules , The framework can identify test cases .

that , Now let's understand pytest Test naming rules in the framework .

Naming rules

In understanding pytest Before the test naming rules , We need to know pytest Rules for searching use cases , Its default search rules are as follows :

  • If pytest The directory is specified in the execution command , Then search the test case file from the specified directory , If not specified , Then start to find the file from the current running Directory , The final result is to find the test cases that conform to the naming rules in the whole project .
  • It will find the test modules that conform to the command rules in the whole project , Again by Test module --> Test class / Test functions --> The test method Recursive search layer by layer .

Default naming rules

pytest The naming rules of the test are as follows :

  • Test module : With test_ Name at the beginning , Such as :test_login.py, Or to _test ending , Such as :login_test.py
  • Test class : Must be Test Name at the beginning , And the test class cannot have init Method
  • The test method / Test functions : Must be test start , Such as :test_login() or testRegister()

It is better to test the module 、 The test method / All functions are written in test_ Name at the beginning , This makes it more readable , Here's the picture :

Custom naming rules

pytest The frame can pass through pytest.ini Configure file custom naming rules , It may be used in some specific scenarios .

Create in the root directory of the test project pytest.ini file , And make the following configuration :

[pytest]
#  Change the naming rules of test modules 
python_files = CS*
#  Change the naming rules of test classes 
python_classes = CS*
#  Change the test method / Test function naming rules 
python_functions = CS*

Be careful , stay .ini When Chinese comments are directly used in the document, an error will be reported , When actually using the above code , Chinese Notes need to be removed .

New test module CS_register.py, Examples are as follows :

import pytest
import requests, json

class CSRegister:

    def CS_register(self):
        ''' Registered users '''
        headers = {"Content-Type": "application/json;charset=utf8"}
        url = "http://127.0.0.1:5000/register"
        data = {
            "username": " Jacky Cheung ",
            "password": "123456",
            "sex": "0",
            "telephone": "13823456789",
            "address": " Dongcheng District, Beijing "
        }
        res = requests.post(url=url, headers=headers, json=data).text
        res = json.loads(res)
        assert res['code'] == 0


if __name__ == '__main__':
    pytest.main()

The results are as follows :

Unless it's a special case , Otherwise, it is not recommended to customize the naming rules .

summary

From personal practical experience , Whether it's pytest still unittest Automation project , The best way to name it is as follows :

  1. Test module name with test start , Such as test_login.py
  2. Test class name with Test start , Such as TestLogin()
  3. Test functions / The method name is test start , Such as test_login()

That's how you name it , Simple and clear , In fact, custom naming is rarely used , Unless the project is special .

原网站

版权声明
本文为[Here is a blank sheet of paper for you]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131223389987.html