我正在尝试在测试用例之间传递参数。我做了一个测试用例,它使用响应{"id":"5f985866f5532a52bb259926","name":"testName","desc":"","descData":null}
进行post调用
我想将"id":"5f985866f5532a52bb259926“作为参数传递给下一个测试用例。
到目前为止,我所拥有的
@pytest.mark.first
def test_create_a_board():
api_board = APIBoard()
create_board = api_board.create_board_call(board_name='testName')
Logger.LogInfo(f"creating a board named {create_board['name']}")
def test_create_3_cards_on_board():
api_board = APIBoard()
create_list = api_board.create_a_list_on_a_board_call()
定义test_create_a_board返回
{"id":"5f985866f5532a52bb259926"}
如何从test_create_a_board向test_create_3_cards_on_board传递{"id":"5f985866f5532a52bb259926"}?从Pytest级别
发布于 2020-10-28 05:08:49
我建议使用类或模块作用域的fixture来创建一个由多个测试共享的变量:
@pytest.fixture(scope="module")
def board():
api_board = APIBoard()
yield api_board.create_board_call(board_name="test_name")
def test_create_a_board(board):
Logger.LogInfo(f"creating a board named {board['name']}")
def test_create_3_cards_on_board(board):
create_list = board.create_a_list_on_a_board_call()
以下是一些注意事项:
这些看起来可能是冒烟测试(即,只运行没有断言以确保没有中断的测试),但您可以考虑在测试中添加断言:
def test_create_a_board(board):
expected = {"expected": "output"}
assert board['name'] == expected
def test_create_3_cards_on_board(board):
create_list = board.create_a_list_on_a_board_call()
assert create_list == ["whatever", "list"]
此外,除非这些测试是专门为测试外部资源而设计的,否则您可以考虑模拟API调用的结果。
@pytest.fixture(scope="module")
def mock_board():
return {"board": "mock"}
或者考虑使用unittest.mock
来模拟您试图测试的对象。
https://stackoverflow.com/questions/64559985
复制相似问题