pytest.mark.parametrize装饰器可以实现测试用例参数化

parametrizing

1.这里是一个实现检查一定的输入和期望输出测试功能的典型例子

# content of test_expectation.py

# coding:utf-8

import pytest
@pytest.mark.parametrize("test_input,expected",
                         [ ("3+5", 8),
                           ("2+4", 6),
                           ("6 * 9", 42),
                         ])
def test_eval(test_input, expected):
    assert eval(test_input) == expected

if __name__ == "__main__":
    pytest.main(["-s", "test_canshu1.py"])

2.若要获得多个参数化参数的所有组合,可以堆叠参数化装饰器 

import pytest
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
def test_foo(x, y):
    print("测试数据组合:x->%s, y->%s" % (x, y))


if __name__ == "__main__":
    pytest.main(["-s", "test_canshu1.py"])

相关文章:

  • 2022-12-23
  • 2021-12-29
  • 2022-12-23
  • 2021-12-29
  • 2021-06-30
  • 2021-11-16
  • 2021-09-15
猜你喜欢
  • 2021-11-03
  • 2021-11-08
  • 2022-01-22
  • 2022-12-23
  • 2022-01-08
  • 2021-08-01
相关资源
相似解决方案