Pytest framework Detailed explanation of the use of files
Document characteristics
- All test files in the same directory will execute the file before running No need to import the import
- The pytest use case will automatically recognize the file and place it in the root directory of the project for global directory calls, and if it is placed under a package, it will be valid within that package, and can have multiple
- Be under the same pakage as the running use case and have the __init__.py file
- Can be called across .py files, and when there are multiple .py files to call, you can have the fixture called only once, or multiple times.
1. Introduction to the document
file is a very important thing inside the Pytest framework, it is possible to write the Fixture function inside this file, the role of this Fixture function is equivalent to the Unittest framework inside the
setup()
Prefunctions andteardown()
post function, although the Pytest framework also has thesetup()
Prefunctions andteardown()
post function, but in practice it is not necessary to write it in the test case file, directly in theInside is just fine, the Pytest framework will automatically go to the
inside the file, which is more flexible.
To summarize: in practice, it is usuallycap (a poem)
@()
Used in combination to enable global front and back applications.
2. The precautions to be taken
-
The file is a Fixture configuration file stored separately and the name cannot be changed.
- Can be used in different
.py
file using the same Fixture function. - cardinal
It needs to be in the same directory as the running use case and have the
__init__.py
file, then theActs on the entire catalog.
- If you want the Fixture to be shared among all tests, you can set the
file in the root directory of the test framework.
-
in the file without having to do any of the
imprt
The import operation will be able to read it because the Pytest use case will look it up automatically. - It is recommended that all Fixtures for a test project be stored in the
file, put the
as Pytest's Fixture repository.
3. Use of
(1) The root directory or package of the test framework creates thefile
""" 1.Learning Objectives Mastery of the document preparation format 2.procedure 2.1 File name cannot be changed file holds all of the project'sfixture convenient forfixtureManagement and maintenance 2.2 In defining the function Before the function, add the@()decorator Pass in the test case's functionfixtureThe name of the function that identifies the。 draw attention to sth.:file in the root directory of the project,The scope is global。 The file is placed under a package,The scope is only within the package。 """ import pytest from selenium import webdriver @() def login(): print("Open the app.") print("Login successful.") yield # When the use case execution is complete, execute the code after the yield. print("Close the app.") @() def driver(request): # request is a keyword in Pytest, fixed write. # Step 1: Create the browser driver object driver = () # Step 3: Define the code to be executed after the use case is executed, encapsulated in a function def end(): # The end function is customized () # Step 4: Execute the code encapsulated above. # End the above function with the request keyword. (end) # Termination functions # Step 2: Return the browser-driven object to the test case return driver
(2) The test case file is as follows
import pytest # Note: To add the fixture method defined in the file to the use case def test_add_cart(login): # Pass the fixture function from the file into the use case print("Add Shopping Cart - Login Required") def test_add_address(login): print("Add shipping address - login required") if __name__ == '__main__': () """ Execution result: test_01.py::test_add_cart Open App Log in successfully Add cart - login required PASSED Close APP test_01.py::test_add_address Open APP LOGIN Succeeded Add shipping address - login required PASSED Close APP """
4. Prioritization of documents in different locations
Its scope is the current directory including test modules in subdirectories.
- For example, in the root directory of the test framework, create the
file, the scope of Fixture in the file is all test modules.
- For example, in a separate test folder create
file, the scope of Fixture in the file is then limited to the test modules in that test folder.
- A test module outside of this test folder, or a test folder outside of this test folder, cannot call this
Fixture in the file.
- If the test framework's root directory and subpackages have the
file, and both
The files all have a Fixture of the same name, which actually takes effect in the subpackage directory of the test framework in the
Fixture configured in the file.
- Tip: You can add more than one use case
The Fixture fixture defined in the file is as follows:
# Note: To add the fixture method defined in the file to the use case def test_add_cart(login, driver): print("Add Shopping Cart - Login Required")
5, in the Fixture ( fixture ) of the scope of action
The scope parameter of the Fixture also appliescharacterization of Fixture in the file:
-
The scope parameter for Fixture in the file is
session
Then all the test files are executed once before (and after) the execution of theFixture in the file.
-
The scope parameter for Fixture in the file is
module
, then each test file is executed once before (and after) the execution of theFixture in the file.
-
The scope parameter for Fixture in the file is
class
, then each test class in the test file is executed once before (and after) the execution of theFixture in the file.
-
The scope parameter for Fixture in the file is
function
, then all the files are executed once before (and after) test case executionFixture in the file.
6. Summary:
- Pytest framework in the
setup()/teardown()
function.setup_class()/teardown_class()
Functions. They are acting on all use cases or all classes. -
@()
The scope of is both partial and full use cases can be placed before and after. -
documents and
@()
Decorators are used in combination and act on the front and back of global use cases.
to this article on the use of Pytest framework file details of the article is introduced to this, more related Pytest file content please search for my previous posts or continue to browse the following related articles I hope that you will support me in the future!
Related articles
Python OpenCV Canny edge detection algorithm principle implementation details
This article introduces the principle of Python OpenCV Canny edge detection algorithm implementation details, because edge detection is sensitive to noise, so the image is applied to the Gaussian smoothing to help reduce the noise, the specific details of the need for small partners can refer to2022-07-07Python 12306 Train Ticket Scripts
This article is mainly for you to introduce in detail the Python 12306 grab train tickets script, with certain reference value, interested partners can refer to it!2018-02-02Generation and Installation of Detailed
This article introduces the detailed generation and installation, the text through the sample code is very detailed, for everyone's learning or work has a certain reference learning value, the need for friends below with the editorial to learn together2023-03-03Python's use of timed scheduling tasks
Python has several ways to schedule a task, which is what we will learn in this article. The next two next will give you an introduction to the use of Python 5 timed scheduling task way, need to friends can refer to the following article content it2021-09-09Getting Started with selenium automation testing in action
This article introduces the selenium automated testing practice, the text of the sample code through the introduction of the very detailed, for everyone to learn or work with some reference to the learning value, the need for friends to learn together with the editorial below it!2020-12-12Python example of an http request using a specified port
Today I'm going to share an example of Python using a specified port for http requests, with good reference value, I hope it will help you. Together follow the editor over to see it2019-07-07Django csrf validation issue implementation
The csrf is used to exploit trusted websites by masquerading as a request from a trusted user. This article mainly introduces the Django csrf authentication issue of the implementation , I think it is quite good , and now share it with you , but also to give you a reference . Together follow the editor over to see it2018-10-10Python process management tool supervisor installation and use of tutorials
supervisor is a process management tool written in python, used to start, restart, shut down processes. The following article mainly gives an introduction to the process management tool supervisor python implementation of the installation and use of related information, need friends can refer to reference, the following take a look together.2017-09-09Python data presentation of the generation of table images
This article mainly introduces the Python data display to generate a table picture, the article is based on Python library information on the subject of a detailed introduction, with some reference value need to partners can refer to it2022-04-04Python Design Patterns Programming Adapter Adapter Pattern Usage Examples
This article introduces the Python design pattern programming in the Adapter adapter pattern use case, generally speaking, the adapter pattern can be subdivided into class adapter and object adapter mode, need friends can refer to the following2016-03-03