1.用例顺序
setup_module
setup_class
setup_method
teardown_method
teardown_class
teardown_module
2. 使用pytest-order控制用例之间的顺序控制,直接引用官方例子:
第一种方法:
import pytest
@pytest.mark.order2
def test_foo():
assert True
@pytest.mark.order1
def test_bar():
assert True
第二种方法:
import pytest
@pytest.mark.second_to_last
def test_three():
assert True
@pytest.mark.last
def test_four():
assert True
@pytest.mark.second
def test_two():
assert True
@pytest.mark.first
def test_one():
assert True
第三种方法:
import pytest
@pytest.mark.run(order=-2)
def test_three():
assert True
@pytest.mark.run(order=-1)
def test_four():
assert True
@pytest.mark.run(order=2)
def test_two():
assert True
@pytest.mark.run(order=1)
def test_one():
assert True
更多用法,请查看官方文档:
https://pytest-ordering.readthedocs.io/en/develop/
3. 参数化以及 打印日志
import pytest
import logging
logging.basicConfig(level=logging.DEBUG)
class TestA:
def setup(self):
logging.info("setup开始执行")
def teardown(self):
logging.info("teardown 完成")
def add(self, a, b):
return (a + b)
@pytest.mark.parametrize("a, b, c", [
(1, 1, 2),
(1, 0, 1),
(1, -1, 2),
(1, 1000000, 1000001)
])
def test_add(self, a, b , c):
print(a,b,c)
assert self.add(a, b)==c
原文:https://www.cnblogs.com/tQlve/p/12016961.html