python learning -pytest (three) -fixture

A, fixture advantages
1, fixture relative to the setup and teardown, it should have the following advantages:
naming flexible setup and teardown is not limited to these named
conftest.py configuration where you can share data, you do not need to be able to import automatically find some configurations
scope = "module" may be implemented across multiple file sharing .py front
scope = "session" in order to achieve a plurality of cross .py file accomplished using a session with a plurality of Example
2, using the function of decorative fixture tags
fixture (scope = "function", params = None, autouse = False, ids = None, name = None)

You can use this decorator (with or without parameter) defined in the fixture to function. Name fixture functions can be used later, citing it calls it before running the tests:

test module or class can be used pytest.mark.usefixtures (fixturename) labeled
test fixture function name can be directly used as an input parameter, in this case, examples of the jig fixture will be returned from the function of injecting
two, fixture parameters introduced
fixture (scope = "function", params = None, autouse = False, ids = None, name = None)

1、scope

There are four levels of scope parameter parameter: "function" (default), "class", "module", "session"

2、params

An optional parameter list, it will lead to more arguments calling fixture functional test and use it all

3, autouse

If True, all testing was activated fixture func can see it

If False (default), the explicit reference to the need to activate the fixture

4、ids

Id list for each string, each string corresponding to the params, ID so that they are part of the test, and if they do not provide an ID generated automatically from params

5、name

The name of the fixture, which defaults to the name decorative function. If it is defined in the fixture using the same module, the function name is requested clamp jig masking function arg; One way to address this problem is a decorative function named "fixture_ <fixturename>", then "@ pytest.fixture (name = '<fixturename>') "

Note: fixtures can choose to use the yield statement to test a function provide their values, rather than return. In this case, as the code block after the code execution detached yield statement, regardless of the test results. fixture function must produce only once

Three, fixtures parameters passed (scope = "function")
1, implementation scenarios: Example 1 with the need to log in, Example 2 does not need to log in, will need to sign with Example 3

# ! = UTF-8 encoding 
Import pytest 
 
# default when no parameters = scope "function" 
@ pytest.fixture ()
 DEF the Login ():
     Print ( " enter the account password to login " ) 
 
DEF test_s1 (the Login):
     Print ( ' use Case 1: other action 111 after logging ' ) 
 
DEF test_s2 ():   # do not pass Login 
    Print ( " use Case 2: no login, operation 222 ' ) 
 
DEF test_s3 (Login):
     Print ( ' with Example 3: other operation after login 333 ' ) 
 
IF  __name__ == "__main__":
    pytest.main('-s test_class004.py')

operation result:

 

 

 

2, if @ pytest.fixture () there is no argument, then the default scope = "function", that is, at this time the level of function, function for effective

3, after the addition of test cases which login parameters, then the login function will first run in the decorative fixture before running a use case

Four, fixture parameter passing (scope = "Module")
1, fixture parameter scope = "module", module role is to take effect throughout the .py file, when you call the use case, write the function name parameter on the line

# ! = UTF-8 encoding 
Import pytest 
 
@ pytest.fixture (scope = " Module " )
 DEF Open ():
     Print ( " open the browser and open the Baidu home page " ) 
 
DEF test_s1 (Open):
     Print ( " execution Example 1 " ) 
 
DEF test_s2 (Open):
     Print ( " execution Example 2 " ) 
 
DEF test_s3 (Open):
     Print ( " execution Example. 3 " ) 
 
IF  the __name__ == " __main__ ":
    pytest.main('-s test_class005.py')

operation result:

 

 

 

Seen from the results, although test_s1, test_s2, test_s3 three local call open function, but it only before the execution of a use case in a

2, if test_s1 do not call, test_s2 call open function, test_s3 not call the run sequence What happens?

# Encoding = UTF-8! 
Import pytest 
 
@ pytest.fixture (scope = " Module " )
 DEF Open ():
     Print ( " open the browser and open the Baidu home page " ) 
 
DEF test_s1 ():
     Print ( " execution Example 1 " ) 
 
DEF test_s2 (Open):
     Print ( " performed with Example 2 " ) 
 
DEF : test_s3 ()
     Print ( " execution Example. 3 " ) 
 
IF  the __name__ == " __main__ ":
    pytest.main('-s test_class005.py')

operation result:

 

 

 

Seen from the results, module-level fixture in the current .py module, the only once before the first call to use case 

Five, conftest.py configuration
1, the above case is in the same .py file, multiple calls to a login function use case, if there are multiple .py files need to call the login function, then you can not log in to write the use case to the inside, this case should have a profile, managed separately preset number of operating scenarios, pytest which reads the default configuration inside conftest.py

conftest.py configuration requires attention to the following points:

conftest.py configuration script name is fixed and can not modify the name
conftest.py use and operation in the same embodiment to a Package, and there init.py file (as long as the pro-test operation conftest.py embodiment the parent directory or file with the same a package can be called that, do not have them in the same package, you can adjust the position of conftest.py test)
does not need to import import conftest.py, pytest with regular automatic lookup

 

 

 

conftest.py # 
 
# encoding = UTF-8! 
Import pytest 
 
@ pytest.fixture () 
DEF the Login (): 
    Print ( 'enter the account password to login') 
 
@ pytest.fixture () 
DEF open_index (): 
    Print ( 'open Home ')

  

# test_fix1.py 
 
# encoding = UTF-. 8! 
Import pytest 
 
DEF test_s1 (Login): 
    Print ( 'used in Example 1: After logging other action 111') 
 
DEF test_s2 (open_index): 
    Print ( 'with Example 2: Open Home operation, operation 222 ') 
 
DEF test_s3 (Login): 
    Print (' with Example 3: other after logging operation 333 ') 
 
IF __name__ __ == "__ main__": 
    pytest.main (' - S test_fix1.py ')

 

# test_fix2.py 
 
# encoding = UTF-. 8! 
Import pytest 
 
DEF test_s4 (Login): 
    Print ( 'with Example 4: After logging other action 111') 
 
DEF test_s5 (open_index): 
    Print ( 'with Example 5: Open the home operation, operation 222 ') 
 
IF __name__ __ == "__ main__": 
    pytest.main (' - S test_fix2.py ')

 

Execution test_fix1.py

 

 

Execution test_fix2.py

 

 

2, with a separate operation test_fix1.py test_fix2.py can call the method conftest.py among the decorator fixture, so that we can achieve some common operations can be invoked separately

3, if the .py file calls conftest.py call will report the following error when less than

==================================== ERRORS ====================================
__________________________ ERROR at setup of test_s1 ___________________________
file /Users/luozelin/Desktop/pytest/pytest_demo/test_fix1.py, line 4
  def test_s1(login):
E       fixture 'login' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, metadata, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.
 
/Users/luozelin/Desktop/pytest/pytest_demo/test_fix1.py:4
__________________________ ERROR at setup of test_s2 ___________________________
file /Users/luozelin/Desktop/pytest/pytest_demo/test_fix1.py, line 7
  def test_s2(open_index):
E       fixture 'open_index' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, metadata, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.
 
/Users/luozelin/Desktop/pytest/pytest_demo/test_fix1.py:7
__________________________ ERROR at setup of test_s3 ___________________________
file /Users/luozelin/Desktop/pytest/pytest_demo/test_fix1.py, line 10
  def test_s3(login):
E       fixture 'login' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, metadata, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.
 
/Users/luozelin/Desktop/pytest/pytest_demo/test_fix1.py:10
=============================== 3 error in 0.03s ===============================

  


Refer to the original link: https: //blog.csdn.net/BearStarX/article/details/101000516

Guess you like

Origin www.cnblogs.com/zhaocbbb/p/12543495.html