Pytest:执行方式

作者: 五娃儿 | 来源:发表于2018-03-20 15:54 被阅读29次

执行方式 为pytest命令行方式+ 通过python代码执行pytest

  • pytest命令行执行
  1. 在控制台执行 pytest


    image.png

    2.在控制台指定执行范围
    a.指定某个模块 pytest test_module.py
    b.指定某个目录及其子目录的所有测试文件 pytest testcase
    c.指定某个某块的某个方法 pytest test_module::test_function
    d.指定执行某模块的某个类中的某个用例 用“::”分割 pytesy test_model.py::test_class::test_method
    更多指定范执行范围样例,请参考官网

  • 通过python代码执行pytest
    1.直接执行pytest.main() 【自动查找当前目录下,以test_开头的文件或者以_test结尾的py文件】
    2.设置pytest的执行参数 pytest.main(['--html=./report.html','test_login.py'])【执行test_login.py文件,并生成html格式的报告】
    方式2中,执行参数和插件参数,通过[]进行分割,[]内的多个参数通过‘逗号,’进行分割
pytest.main源码
通过源码可以看出,main支持两个参数,即执行参数和插件参数

def main(args=None, plugins=None):
    """ return exit code, after performing an in-process test run.

    :arg args: list of command line arguments.

    :arg plugins: list of plugin objects to be auto-registered during
                  initialization.
    """
    try:
        try:
            config = _prepareconfig(args, plugins)
        except ConftestImportFailure as e:
            tw = py.io.TerminalWriter(sys.stderr)
            for line in traceback.format_exception(*e.excinfo):
                tw.line(line.rstrip(), red=True)
            tw.line("ERROR: could not load %s\n" % (e.path), red=True)
            return 4
        else:
            try:
                return config.hook.pytest_cmdline_main(config=config)
            finally:
                config._ensure_unconfigure()
    except UsageError as e:
        tw = py.io.TerminalWriter(sys.stderr)
        for msg in e.args:
            tw.line("ERROR: {}\n".format(msg), red=True)
        return 4

相关文章

网友评论

    本文标题:Pytest:执行方式

    本文链接:https://www.haomeiwen.com/subject/tlzhfftx.html