SoFunction
Updated on 2024-10-30

Pytest framework Detailed explanation of the use of files

Pytest framework Detailed explanation of the use of files

Updated June 02, 2022 09:15:00 by Test-Buster
pytest is unique to the local test configuration file, can be used to set up project-level fixture, can also be used to import external plug-ins, this article introduces the use of Pytest framework file, interested friends take a look together!

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 thesetup()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 theinside 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

  1. The file is a Fixture configuration file stored separately and the name cannot be changed.
  2. Can be used in different.pyfile using the same Fixture function.
  3. cardinalIt needs to be in the same directory as the running use case and have the__init__.pyfile, then theActs on the entire catalog.
  4. If you want the Fixture to be shared among all tests, you can set thefile in the root directory of the test framework.
  5. in the file without having to do any of theimprtThe import operation will be able to read it because the Pytest use case will look it up automatically.
  6. It is recommended that all Fixtures for a test project be stored in thefile, put theas 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 thefile, the scope of Fixture in the file is all test modules.
  • For example, in a separate test folder createfile, 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 thisFixture in the file.
  • If the test framework's root directory and subpackages have thefile, and bothThe files all have a Fixture of the same name, which actually takes effect in the subpackage directory of the test framework in theFixture configured in the file.
  • Tip: You can add more than one use caseThe 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 issessionThen 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 ismodule, 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 isclass, 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 isfunction, then all the files are executed once before (and after) test case executionFixture in the file.

6. Summary:

  • Pytest framework in thesetup()/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!

  • Pytest
  • file

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 to
    2022-07-07
  • Python 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-02
  • Generation 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 together
    2023-03-03
  • Python'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 it
    2021-09-09
  • Getting 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-12
  • Python 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 it
    2019-07-07
  • Django 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 it
    2018-10-10
  • Python 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-09
  • Python 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 it
    2022-04-04
  • Python 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 following
    2016-03-03

Latest Comments