Scenes:
Py file for a certain pre-conditions require use cases without the need to precondition certain use cases, using setup / teardown is certainly inconvenient,
Then you need to customize the pre-conditions of the test case.
1, fixture advantages:
- Naming is not limited to the setup, teardown these can be arbitrarily
- conftest.py can share data files, automatically do not need to be able to find some configuration import import
- scope = "module" can implement multiple .py file sharing across the front, each .py file called once
- scope = "session" in order to achieve a plurality of cross .py file accomplished using a session with a plurality of cases
2, fixture parameters into (scope = 'function')
For function effectively, it is necessary precondition incoming fixture on the line
There are four levels of scope parameter "function " (default), " class", " Module" or " the session"
The following example: Example 1 with the need to log in, log 2 need not use cases, need to be logged with Example 3
# Create a new file test_fixt.py # Coding: UTF-8 Import pytest when no parameter # = default scope "function" @ pytest.fixture () DEF the Login (): Print ( "enter the account password to login") DEF test_s1 (Login): Print ( "used in Example 1: after logging other actions 111") DEF test_s2 (): # do not pass Login Print ( "use Case 2: no login operation 222") DEF test_s3 (Login): Print ( "use case 3: after logging 333 other actions ")
3, conftest.py profile
The above is a case in the same .py file, multiple use cases call a sign-on function, if there are multiple .py files need to call the login function, then it can not be written landed use cases inside.
In this case should have a profile, managed separately preset number of operating scenarios, pytest which reads the default configuration inside conftest.py
conftest.py arrangement Notes
- conftest.py configuration script name is fixed and can not change the name
- conftest.py use and operation in the same embodiment to a pakage, and there __init__.py file
- You do not need to import import conftest.py, pytest with regular automatic lookup
__init__.py conftest.py # Coding: UTF-8 Import pytest @ pytest.fixture () DEF the Login (): Print ( "enter the account password to login") test_fix1.py # Coding: UTF-8 Import pytest DEF test_s1 ( Login): Print ( "used in Example 1: after logging other actions 111") DEF test_s2 (): # do not pass Login Print ( "use Case 2: no login operation 222") DEF test_s3 (Login): Print ( "use Case 3 : after logging other actions 333 ") IF the __name__ ==" __main__ ": pytest.main ([" - S "," test_fix1.py "]) test_fix2.py # Coding: UTF. 8- Import pytest DEF test_s4 (Login): print ( "Use Case 4: Other Action 111 "after logging) def test_s5 (): # do not pass Login Print ( "Example 5 with: no login operation 222") IF the __name__ == "__main__": pytest.main ([ "- S", "test_fix2.py"])
Test_fix1.py run separately and test_fix2.py can call the login () method, so that we can achieve some common operations can separate out the
(Reference lengthy blog)