Pytest 2 @pytest.fixture运用及对map结果的渐进测试方法

作者: gavin 分类: Python,自动化测试 发布时间: 2020-11-07 00:00

1、单文件实现效果

def greet(person):
    return "Hi {name}".format(**person)

def test_greet():
    bob = {"name":"Bob"} #Arrange
    greeting = greet(bob)  #Act

    assert greeting == "Hi Bob"  #Assert

2、拆分文件实现方式 关键词:@pytest.fixture

fun.py

def hi(person):
    return "Hi {name}".format(**person)

def bye(person):
    return "Bye {name}".format(**person)

def how_are_you(person):
    return "How are you {name}?".format(**person)

test_pass.py

from fun import hi, bye, how_are_you

import pytest

@pytest.fixture

def test_hello():
    bob = {"name": "Bob"}
    assert hi(bob) == "Hi, Bob"


def test_bye():
    bob = {"name": "Bob"}
    assert bye(bob) == "Bye Bob"

def test_how_are_you():
    bob = {"name": "Bob"}
    assert how_are_you(bob) == "How are you Bob?"


3、定义bob方法传值

from test_case.fun import hi, bye, how_are_you

import pytest

@pytest.fixture

def bob():
    return {"name": "Bob"}

def test_hello(bob):

    assert hi(bob) == "Hi Bob"


def test_bye(bob):

    assert bye(bob) == "Bye Bob"

def test_how_are_you(bob):

    assert how_are_you(bob) == "How are you Bob?"