pytest - 测试流程顺序 [英] pytest - test flow order

查看:50
本文介绍了pytest - 测试流程顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于下面的 pytest 代码...如果我使用 --count 3 运行它,它将运行 test_first 3 次,然后运行 ​​test_second 3 次.

I have a pytest code similar to below... if i run it with --count 3 it will run the test_first 3 times then test_second 3 times.

如果我希望它运行 test_first、test_second 并重复该流程怎么办?

What if i would like it to run test_first, test_second and repeat that flow?

谢谢.:)

@pytest.mark.usefixtures('setup')
class TestSomething:

    def run_setup(self):
        pass

    def test_first(self):
        print('test 1')
        name = 'name'
        assert name.isalpha()

    def test_second(self):
        print('test 2')
        name = '12345'
        assert name.isalpha()

推荐答案

你可以自己实现.查看pytest_collection_modifyitems 钩子,您可以在其中更改要执行的测试列表.示例:

You can implement it yourself. Take a look at the pytest_collection_modifyitems hook where you can alter the list of tests to be executed. Example:

# conftest.py
import pytest

def pytest_addoption(parser):
    parser.addoption('--numrepeats', action='store', type=int, default=1)

def pytest_collection_modifyitems(items):
    numrepeats = pytest.config.getoption('--numrepeats')
    items.extend(items * (numrepeats - 1))

当放入测试根目录中的 conftest.py 文件时,此代码添加了一个新的命令行选项 numrepeas 将重复测试运行 n 次:

When put into a conftest.py file in the tests root dir, this code adds a new command line option numrepeats that will repeat the test run n times:

$ pytest --numrepeats 3

这篇关于pytest - 测试流程顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆