pytest使用方法

一、编写规则

编写pytest测试样例非常简单,只需要按照下面的规则:

  • 测试文件以test_开头(以_test结尾也可以)
  • 测试类以Test开头,并且不能带有 init 方法
  • 测试函数以test_开头
  • 断言使用基本的assert即可

二、插件

pytest具有很多第三方插件,并且可以自定义扩展,比较好用的如pytest-selenium(集成selenium)、pytest-html(完美html测试报告生成)、pytest-rerunfailures(失败case重复执行)、pytest-xdist(多CPU分发)等;

三、执行测试用例

  • pytest                                            # 运行当前目录下所有以test_开头或_test 结尾的测试用例
  • pytest test_mod.py                        # 运行指定测试文件
  • pytest somepath                            # 运行指定目录下面的测试文件
  • pytest -k stringexpr # only run tests with names that match the
    # the "string expression", e.g. "MyClass and not method"
    # will select TestMyClass.test_something
    # but not TestMyClass.test_method_simple
  • pytest test_mod.py::test_func # only run tests that match the "node ID",
    # e.g "test_mod.py::test_func" will be selected
    # only run test_func in test_mod.py
posted @ 2021-03-12 17:43  远洪  阅读(340)  评论(0编辑  收藏  举报