Fixture of pytest


Pytest usage rules:

1. Test is used at the beginning of the file_ start

2. The test class starts with test

3. Test function with test_ start

4. Assert


Fixture: fixture code

1. Make initialization settings before and after the test, such as test data preparation, linking the database, opening the browser, etc

2. Preconditions of test cases can be realized by fixture

3. Be able to use step and teardown like Unitest

4. Fixturn can realize the functions that unittest cannot achieve. For example, test cases and test cases in unittest cannot transfer parameters and data, but fixturn can

Detailed explanation:

1、 It is also named independently and activated by declaring that they are used from the whole project of the function module class

Two, modular implementation, each fixture can call each other.

3、 Fixtures can be extended from simple units to complex functions, allowing configuration and components. Parameterize the picture and test case, or cross function function class module moudle test session

In the case of failure in the use case, failure is failure

1. Pass in as a parameter:


When other functions call the function marked by fixture and the parameter is passed in directly, the function marked with fixture () should not use test_ start

The use case fixture has no reture, and the return value is none



def login():




def test_login(login)

  asser p!=-1,u!=-1


2. Use @ pytest mark. Usefixtures (‘fixture), the function in the following class will automatically bring in the marked function arguments



def login():




class Test_login():

  def test_login():

    assert p!=1

  def test_login2():

    assert u!=1

3. Autouse parameter, you can set the effective range

@python. Top note: fixture (scope= “calling range”, “autouse=Ture”). [class: each class runs once, the case is called before running; session: is called by multiple.Py files; function: runs every function method; moudle: runs once for each module.



@python. Fixture() does not write the range. The default is function

def psw():



def test_login(psw):


  assert psw==123456


@python. Fixture (scope =’class’) if there are many use cases in the class that call fixture, the fixture will be executed once before all use cases in the class, but the values in it can be used all the time


@python. Fixture (scope =’mode ‘) runs once before all use cases in the module start, and the values in it are available


@python. The fixture (scope =’session ‘) session level can span multiple sessions Py file module calls. If multiple py files need to be called once, you can set it to scope = session and write it to conf test Py lower


conftest. If the PY file name is fixed, pytest will automatically identify the file and put it in the project root directory, it can be called globally. If it is put under its package package, it is only valid in the package and can be called directly in two files


Multiple fixtures interact directly

1. Use


Recommended Today

About interpersonal relationships

Not long ago, a classmate told me that his interpersonal relationship was very poor since he was a child. Few people in school played well. Usually he was alone. He had long recognized that this situation was bad, but he was also used to it.   But now that I have graduated, I am about […]