我有一些带有测试的脚本,我需要在 中运行这些测试我明确定义的执行顺序 .
看起来像:
# one.py
import some lib
class Foo():
def makesmth(self)
script
然后我制作了测试文件:
# test_one.py
import pytest
import some lib
class TestFoo():
def test_makesmth(self):
try/except/else assert etc.
所以它看起来简单而正确。当我运行文件 test_one.py 时一切正常。
我的脚本测试包看起来像:
package/
|-- __init__.py
|-- scripts
| |-- one.py
| |-- two.py
|-- tests
| |-- test_one.py
| |-- test_two.py
当我尝试收集测试时
pytest --collect-only
它给出了非字母顺序和随机的测试顺序。
我可以在哪里写有关测试顺序的信息?非字母顺序,就像我想像 b、a、c、e、d 一样开始测试 - 不是随机的,不是按字母顺序的
试图制作文件tests.py:
import pytest
from tests.test_one import TestFoo
from tests.test_two import TestBoo etc.
当我尝试运行它时,会显示错误,因为这些导入是以我不理解的方式完成的(试图使 a TestFoo b TestBoo 并重命名以这种方法定义方式测试文件,但它仍然不起作用)。
对不起,如果我的问题看起来不专业,我是初级问答,几乎没有自动测试经验。
最佳答案
您可以使用 pytest-ordering
见 https://pytest-ordering.readthedocs.io/en/develop/
import pytest
@pytest.mark.run(order=1)
def test_first():
pass
@pytest.mark.run(order=2)
def test_second():
pass
test_sample.py::test_first PASSED
test_sample.py::test_second PASSED
关于python-3.x - Pytest - 测试用例执行顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53887011/