跟着官方文档理解Fixture用法(一)

Fixture用法1:

当做参数传入测试方法中

先看官方文档的示例代码:

import pytest


class Fruit:
    def __init__(self, name):
        self.name = name

    def __eq__(self, other):
        return self.name == other.name


@pytest.fixture
def my_fruit():
    return Fruit("apple")


@pytest.fixture
def fruit_basket(my_fruit):
    return [Fruit("banana"), my_fruit]


def test_my_fruit_in_basket(my_fruit, fruit_basket):
    assert my_fruit in fruit_basket

At a basic level, test functions request fixtures by declaring them as arguments, as in the test_my_fruit_in_basket(my_fruit, fruit_basket): in the previous example.

At a basic level, pytest depends on a test to tell it what fixtures it needs, so we have to build that information into the test itself. We have to make the test “request” the fixtures it depends on, and to do this, we have to list those fixtures as parameters in the test function’s “signature” (which is the def test_something(blah, stuff, more): line).

When pytest goes to run a test, it looks at the parameters in that test function’s signature, and then searches for fixtures that have the same names as those parameters. Once pytest finds them, it runs those fixtures, captures what they returned (if anything), and passes those objects into the test function as arguments.

在基本级别上,测试函数通过将fixture声明为参数来请求fixture,如test_my_fruit_in_basket(my_fruit, fruit_basket):在前面的示例中。

在基本层面上,pytest依赖于测试来告诉它它需要什么fixture,因此我们必须将这些信息构建到测试本身中。我们必须让测试“请求”它所依赖的fixture,为此,我们必须将这些fixture作为参数列在测试函数的“签名”(函数def test_something()的参数)中。

当pytest运行测试时,它会查看该测试函数签名中的参数,然后搜索与这些参数具有相同名称的fixture。一旦pytest找到这些对象,它就会运行这些fixture,捕获它们返回的内容(如果有的话),并将这些对象作为参数传递给test函数。

Fixture用法2:

Fixture可以调用其他的fixture

One of pytest’s greatest strengths is its extremely flexible fixture system. It allows us to boil down complex requirements for tests into more simple and organized functions, where we only need to have each one describe the things they are dependent on. We’ll get more into this further down, but for now, here’s a quick example to demonstrate how fixtures can use other fixtures:

# contents of test_append.py
import pytest

# Arrange
@pytest.fixture
def first_entry():
    return "a"

# Arrange
@pytest.fixture
def order(first_entry):
    return [first_entry]

def test_string(order):
    # Act
    order.append("b")

    # Assert
    assert order == ["a", "b"]

Fixture用法3:

Fixture是可以重复使用的,不同的测试用例可以请求同一个fixture,并接收返回各自不同的处理结果,不会相互影响

One of the things that makes pytest’s fixture system so powerful, is that it gives us the abilty to define a generic setup step that can reused over and over, just like a normal function would be used. Two different tests can request the same fixture and have pytest give each test their own result from that fixture.

This is extremely useful for making sure tests aren’t affected by each other. We can use this system to make sure each test gets its own fresh batch of data and is starting from a clean state so it can provide consistent, repeatable results.

# contents of test_append.py
import pytest

# Arrange
@pytest.fixture
def first_entry():
    return "a"

# Arrange
@pytest.fixture
def order(first_entry):
    return [first_entry]

def test_string(order):
    # Act
    order.append("b")

    # Assert
    assert order == ["a", "b"]

def test_int(order):
    # Act
    order.append(2)

    # Assert
    assert order == ["a", 2]
posted @ 2021-03-27 14:48  Hei蛋炒饭  阅读(173)  评论(0)    收藏  举报