Use of Test Naming Rules for Pytest

  • 2021-10-27 07:52:33
  • OfStack

Background:

pytest searches for test cases according to specific rules, so the names of test case files, test classes, methods and test functions in classes must conform to the rules before they can be searched by pytest and added to the test run queue.

Default search rules:

If the pytest command line has a specified directory, the test case files are searched from that directory, and if not, the files are searched from the current running directory. Note that this lookup is recursive, and files in subdirectories will also be found. Not all files in the directory can be found, only files that conform to the naming rules will be found. The default rule is a. py file that begins with test_ or ends with _test. Look for classes beginning with Test and methods beginning with test_ in the class, and functions beginning with test_ in the test file.

Default naming rules for test cases

Unless the pytest command is specified to a test case file, the test case file name should begin with test_ or end with _ test. Test function naming, test class method naming should start with test_. The test class name should begin with Test.

tips: Test classes should not have constructors.

I am used to installing test case folders, test case files, test functions, and test methods in classes all start with test_. It is suggested to keep a unified style.

Example:


# func.py
def add(a,b):
 return a+b

# ./test_case/test_func.py
import pytest
from func import *

class TestFunc:

 #def __init__(self):
  #self.a = 1

 def test_add_by_class(self):
  assert add(2,3) == 5


def test_add_by_func():
 assert add(4,6) == 10

'''
# stdout:
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1
rootdir: D:\Python3.7\project\pytest
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collected 2 items

test_case\test_func.py ..                                                [100%]

============================== 2 passed in 0.04s ==============================
[Finished in 1.3s]
######################################################################
'''

In the test results, test_case\ test_func. py …. Two dots represent two test cases.

Error demonstration, when the test class has a constructor:


# func.py
def add(a,b):
 return a+b

# ./test_case/test_func.py
import pytest
from func import *

class TestFunc:

 def __init__(self):
  self.a = 1

 def test_add_by_class(self):
  assert add(2,3) == 5


def test_add_by_func():
 assert add(4,6) == 10

'''
# stdout:
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1
rootdir: D:\Python3.7\project\pytest
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collected 1 item

test_case\test_func.py .                                                 [100%]

============================== warnings summary ===============================
test_case\test_func.py:4
  D:\Python3.7\project\pytest\test_case\test_func.py:4: PytestCollectionWarning: cannot collect test class 'TestFunc' because it has a __init__ constructor (from: test_case/test_func.py)
    class TestFunc:

-- Docs: https://docs.pytest.org/en/latest/warnings.html
======================== 1 passed, 1 warning in 0.04s =========================
[Finished in 1.4s]
######################################################################
'''

An error will be reported. pytest can only find the function beginning with test_, but cannot find the test class with constructor beginning with Test.

Custom test case naming rules

If, for some reason, test files, test functions, test classes, and test class methods need to be named using other naming conventions, you can do so through the pytest. ini configuration file.

Create the pytest. ini file in the top-level directory of the test system and write the following configuration to the pytest. ini file:


[pytest]
#  Change the test file naming convention 
python_files = HG*

#  Change the test class naming convention 
python_classes = HG*

#  Better test function naming rules 
python_functions = HG*

Example:


# func.py
def add(a,b):
 return a+b

# ./test_case/HG_func.py
import pytest
from func import *

class HGFunc:

 #def __init__(self):
  #self.a = 1

 def HG_add_by_class(self):
  assert add(2,3) == 5


def HG_add_by_func():
 assert add(4,6) == 10

'''
stdout:
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1 -- D:\Python3.7\python.exe
cachedir: .pytest_cache
rootdir: D:\Python3.7\project\pytest, inifile: pytest.ini
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collecting ... collected 2 items

test_case/HG_func.py::HGFunc::HG_add_by_class PASSED                     [ 50%]
test_case/HG_func.py::HG_add_by_func PASSED                              [100%]

============================== 2 passed in 0.03s ==============================
[Finished in 1.3s]
'''

Tips:

pytest. ini is a configuration file that can change the operation mode of pytest, but under normal circumstances, pytest. ini file does not need to exist in the test system at all, so we can work by using the default operation mode. pytest. ini has many other personalized configurations. When necessary, you can create pytest. ini files in the top-level directory of automated test projects and add configurations to achieve the purpose of personalized operation.

Related articles: