颠覆你的Python实践软件测试之路测试Inbox

Pytest:执行方式

2018-03-20  本文已影响29人  五娃儿

执行方式 为pytest命令行方式+ 通过python代码执行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
    更多指定范执行范围样例,请参考官网

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

上一篇 下一篇

猜你喜欢

热点阅读