当前位置:网站首页>Introduction and use of pytest fixture, confitest and mark

Introduction and use of pytest fixture, confitest and mark

2022-06-22 00:43:00 Don't ask me y

Preface
stay b Learning videos found on the website , The title is pytest Frame learning , Followed for a period of time , When writing test cases, I found that the following parentheses of the class say unittest.Testcase, Now I know pytest yes unittest Upgraded version , What are the specific differences? I will add them after my follow-up study

1、pytest Use under the frame fixture,unittest Next use setup、teardown( I'm not sure if this is right )

2、fixture Introduce
fixture Defines the steps and data that make up the test preparation phase
2.1 fixture Fixed writing :

@pytest.fixture(scope = 'function',autouse = ‘True')
def my_fixture():  
	print("a")

perhaps

@pytest.fixture(scope = 'function',autouse = ‘True')
def my_fixture(request):  
		print(' In front of ’)
		# Add ,yield It acts as a dividing line , Separate front and rear 
		yiled request.pram //yield  and return It means to return , however yield You can continue to write code later ,return Not in the back 
		print(‘ After ’)

2.2 fixture Marked functions , Cannot follow test function 、 Put the test classes together , I put fixture The method of marking is written as test_ start , It's not right
2.3 fixture Use

  • The marked function is passed into the test case as a parameter
@pytest.fixture()
def my_fixture():
print ("a")

def test_01(my_fixture):
print(" Use fixture One way “)
  • Use decorators :@pytest.mark.usefixtures(" ")

3、conftest

  • It's all fixture
  • fixture It can be shared externally - All use cases share
  • priority : Nearby principle
  • You can create... Hierarchically
  • It can't be put together with the test case ( Can't use test_) Name the marked function

4、mark: Mark , Need to be in pytest.ini The document states the mark
pytest.ini Marked in ,-m Indicates that only the marked test cases are run
main Write in :pytest.main(["-m smoke","-v"]( Multiple labels can be placed )
Marking range : The test case , Test class , modular

class TestCase(object):
# All use cases under this class will have this tag 
	pytestmark = pytest.mark.xxx
	# Multi label mode 
	pytestmark = [pytest.mark. label 1,pytest.mark. label 2]
	

# All use cases under this module will have this tag 
import pytest
	pytestmark = pytest.mark.xxx
	# Multi label mode 
	pytestmark = [pytest.mark. label 1,pytest.mark. label 2]

5、 Failure rerun mechanism
Installing a plug-in :pip install pytest-rerunfailures
Usage mode :pytest --returns 2 Express : A case that fails to run can be run twice
Time delay :pytest --reruns 2 --reruns-delay x( second )

6、main Parameters in

pytest.main(["-s","-v",                                          # Show detailed use case information 
					"-m"," Mark 1”,                               # The run is marked 1 The use case 
					”-m  Mark 1 and  Mark 2“                         #   Operation marked 1 And marked 2 The use case 
					“--html= report.html",                       # Output html Test report 
					"--alluredir = allure_dir",                  # Output allure Test report 
					"--renruns","2","--reruns-delay","5"])       # Marked with a mark 1 Failure case interval of 5 Second run 2 Time 
原网站

版权声明
本文为[Don't ask me y]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206212326232093.html