pytest in python Collects Use Case Rules and Runs Specified Use Case Details

  • 2021-07-03 00:37:17
  • OfStack

Preface

In the last article, I believe you have learned how pytest runs test cases with various command line parameters under cmd and outputs the information we want to see. So today, I will explain how pytest collects the use cases we wrote. How do we run individual use cases or run use cases in batches? The following will answer for everyone 11!

Analysis of pytest Collection Use Case Principle

First, we create a new project according to the following directory structure


[pyttest Rules for searching test cases ]
|[ Test case directory 1]
| |__init__.py
| |test_ Test module 1.py
| |test_ Test module 2.py
|[ Test case directory 2]
| |__init__.py
| |test_ Test case 1.py
| | Test case .py
|test_ Test module .py
| Test case 2.py 

Code instance


# test_ Test module 1.py
def test_testFunc1():
print('\n I am 1 Test cases ! in test_testFunc1')
assert 1 == 1
def func1():
print(' I'm not 1 Test cases ')
assert 1 == 1
# test_ Test module 2.py
class TestClass1(object):
def test_class_func1(self):
print('\n  I am 1 Test cases in a class  in test_class_func1')
assert 1 == 1
def class_func1(self):
print(' I'm in the class 1 A common function! ')
# test_ Test case 1.py
class TestClass2(object):
def test_class_func2(self):
print('\n  I am 1 Test cases in a class  in test_class_func2',)
assert 1 == 1
def class_func2(self):
print(' I'm in the class 1 A common function! ')
def test_testFunc2():
print('\n I am 1 Test cases  in test_testFunc2!')
assert 1 == 1
def func2():
print(' I'm not 1 Test cases ')
assert 1 == 1
#  Test case .py
def test_testFunc3():
print('\n I am 1 Test cases ! in  Test case .py')
assert 1 == 1
def func3():
print(' I'm not 1 Test cases ')
assert 1 == 1
# test_ Test module 3.py
def test_testFunc4():
print('\n I am 1 Test cases ! in test_testFunc4')
assert 1 == 1
def func4():
print(' I'm not 1 Test cases ')
assert 1 == 1
class TestClass3(object):
def test_class_func3(self):
print('\n  I am 1 Test cases in a class  in test_class_func3')
assert 1 == 1
def class_func3(self):
print(' I'm in the class 1 A common function! ')
#  Test case 2.py
def test_testFunc5():
print('\n I am 1 Test cases ! in test_testFunc5')
assert 1 == 1
def func5():
print(' I'm not 1 Test cases ')
assert 1 == 1

Let's use the cmd command to execute 1 of this project to see how many use cases are valid. Open cmd and switch to the root directory of the project to execute the command pytest -v


D:\pytest Search test case rules >pytest -v
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'}
rootdir: D:\pytest Search test case rules , inifile:
plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10
collected 6 items

test_ Test module 3.py::test_testFunc4 PASSED [ 16%]
test_ Test module 3.py::TestClass3::test_class_func3 PASSED [ 33%]
 Test case directory 1/test_ Test module 1.py::test_testFunc1 PASSED [ 50%]
 Test case directory 1/test_ Test module 2.py::TestClass1::test_class_func1 PASSED [ 66%]
 Test case directory 2/test_ Test case 1.py::TestClass2::test_class_func2 PASSED [ 83%]
 Test case directory 2/test_ Test case 1.py::test_testFunc2 PASSED [100%]
========================== 6 passed in 0.59 seconds ===========================

The running result can be seen that there are 6 use cases passed in 1, and which 6 cases are listed in detail, so there are actually more than 6 use cases written according to us above, so why only 6 use cases are running? Comparing the above code structure with our execution results, we should be able to find such a rule

pytets will look for all directories starting from the directory where we are currently running, and look for files beginning with test_ and all functions beginning with test_ in the files, classes beginning with Test and functions beginning with test_ in the classes as test cases. That's why there are 6 test cases running above!

pytest Run Specified Test Cases

We still use the above project as a demonstration (cdm switches to the root directory of the project)

1. Run all use cases in the specified directory
We specify to run all the use cases in Test Case Directory 1 (pytest-v Test Case Directory 1)


D:\pytest Search test case rules >pytest -v  Test case directory 1
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'}
rootdir: D:\pytest Search test case rules , inifile:
plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10
collected 2 items
 Test case directory 1/test_ Test module 1.py::test_testFunc1 PASSED [ 50%]
 Test case directory 1/test_ Test module 2.py::TestClass1::test_class_func1 PASSED [100%]

========================== 2 passed in 0.05 seconds ===========================
#  This will only search and specify all the uses under the specified directory 

2. Run all use cases in the specified file

We specify to run test_ Test Module 1. py (pytest-v Test Case Directory 1/test_ Test Module 1. py)


D:\pytest Search test case rules >pytest -v  Test case directory 1/test_ Test module 1.py
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'}
rootdir: D:\pytest Search test case rules , inifile:
plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10
collected 1 item
 Test case directory 1/test_ Test module 1.py::test_testFunc1 PASSED [100%]

========================== 1 passed in 0.09 seconds ===========================
#  Run all use cases under the specified file 

3. Run the test class in the specified file

We specify to run the test class Testclass1 in test_ Test Module 2. py (pytest-v Test Case Directory 1/test_ Test Module 2. py:: TestClass1)


D:\pytest Search test case rules >pytest -v  Test case directory 1/test_ Test module 2.py::TestClass1
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'}
rootdir: D:\pytest Search test case rules , inifile:
plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10
collected 1 item
 Test case directory 1/test_ Test module 2.py::TestClass1::test_class_func1 PASSED [100%]

========================== 1 passed in 0.05 seconds ===========================
#  Runs all tests in the specified test class with 

4. Run the specified test case function

We specified to run test_testFunc1 (pytest-v Test Case Directory 1/test_ Test Module 1.py:: test_testFunc1)


D:\pytest Search test case rules >pytest -v  Test case directory 1/test_ Test module 1.py::test_testFunc1
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'}
rootdir: D:\pytest Search test case rules , inifile:
plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10
collected 1 item
 Test case directory 1/test_ Test module 1.py::test_testFunc1 PASSED [100%]

========================== 1 passed in 0.03 seconds ===========================

Summarize

Collect use case rules: Search all test files starting with test_, test classes starting with Test, and test functions starting with test_

Execute use case rules: From the execution information output by-v parameter, we should be able to find that the use case under the specified directory can be run using the command pytest directory/directory; Run the specified file using pytest directory/file; Run the specified class or function using the command pytest Directory/File:: Class Name:: Function Name or pytest Directory/File:: Function Name

Search use case rules are also the rules for naming use case files, test classes and test functions. Execute the specified test case memory rule


Related articles: