1、pytest.approx
断言两个数字(或两组数字)在某个容差范围内彼此相等。
from pytest import approx def test_one(): assert 0.1 + 0.2 == 0.3 # 断言两个数字(或两组数字)在某个容差范围内彼此相等。 def test_two(): assert 0.1 + 0.2 == approx(0.3) def test_three(): assert (0.1 + 0.2, 0.2 + 0.4) == approx((0.3, 0.6)) def test_four(): assert {'a': 0.1 + 0.2, 'b': 0.2 + 0.4} == approx({'a': 0.3, 'b': 0.6})
2、pytest.fail,pytest.skip,pytest.xfail
将用例置为失败,跳过用例,用例预期失败。
import pytest def test_one(): pytest.fail(msg='使用给定消息显式地设置用例为失败状态') def test_two(): pytest.skip(msg='使用给定消息跳过测试用例。') def test_three(): pytest.xfail(reason='由于给定的原因,强制标记失败测试用例或测试准备函数。')
3、pytest.importorskip
导入并返回请求的模块名,如果无法导入模块,则跳过当前测试。
import pytest # 导入并返回请求的模块名,如果无法导入模块,则跳过当前测试。 def test_one(): mod1 = pytest.importorskip(modname='selenium') print(mod1) def test_two(): mod2 = pytest.importorskip(modname='python-module') print(mod2)
4、pytest.exit
退出测试
import pytest def test_one(): pass def test_two(): pass pytest.exit(msg='退出测试,注意test_two也没有执行') def test_three(): pass
5、pytest.main
主函数模式
import pytest def test_one(): pass def test_two(): pass # 主函数模式运行测试用例 if __name__ == '__main__': pytest.main()
6、pytest.param
与mark.parametrize()一起使用,标记参数化的子用例。
import pytest # 和parametrize结合使用,指定参数,标记子用例预期失败状态XFAIL @pytest.mark.parametrize("test_input,expected", [("3+5", 8), pytest.param("6*9", 42, marks=pytest.mark.xfail)]) def test_eval(test_input, expected): assert eval(test_input) == expected
7、pytest.raises
with pytest.raises(),结合上下文管理器,用来捕获异常。
import pytest exception_tuple = (ZeroDivisionError, AssertionError) # with pytest.raises(),结合上下文管理器,用来捕获异常 def test_one(): with pytest.raises(AssertionError) as exc_info: assert 1 == 2 print(exc_info) # 多个异常元组 def test_two(): with pytest.raises(exception_tuple) as exc_info: print(1 / 0) # 如果代码块没有引发预期的异常AssertionError,则置为失败FAILED def test_three(): with pytest.raises(ZeroDivisionError): assert 1 == 2 # 如果代码块没有异常,,则置为失败FAILED def test_four(): with pytest.raises(exception_tuple): assert 1 == 1
8、pytest.warns
with pytest.warns() ,结合上下文管理器,可以检查代码是否发出特定的警告信息,使用方法与 raises 类似。
import warnings import pytest def my_warning(): warnings.warn("my warning", UserWarning) # with pytest.warns() ,结合上下文管理器,可以检查代码是否发出特定的警告信息,使用方法与 raises 类似。 def test_warning(): with pytest.warns(UserWarning): my_warning()
9、pytest.deprecated_call
with deprecated_call(),结合上下文管理器,可用于确保代码块触发 DeprecationWarning 或 PendingDeprecationWarning:
import warnings from pytest import deprecated_call def api_call_v2(): warnings.warn('use v3 of this api', DeprecationWarning) return 200 # with deprecated_call(),结合上下文管理器,可用于确保代码块触发 DeprecationWarning 或 PendingDeprecationWarning: def test_deprecated_call(): with deprecated_call(): assert api_call_v2() == 200
10、pytest.register_assert_rewrite
参考: 断言重写。
register_assert_rewrite(*names): 注册一个或多个要在导入时重写的模块名称。
此函数将确保此模块或程序包内的所有模块将重写其assert语句。因此,您应确保在实际导入模块之前调用此方法,如果您是使用包的插件,则通常在__init__.py中调用。
抛出:TypeError- 如果给定的模块名称不是字符串。
11、pytest.freeze_includes
参考: 冻结pytest。
freeze_includes(): 返回pytest使用的模块名称列表,应由cx_freeze包含。
reference:
API Reference — pytest documentation