pytest入门二:用例的执行顺序

import sys

import pytest

class TestDemo:

    def test_demo1(self):
        x = "hello world"
        print(f"{x} python")
        assert 'h' in x

    def test_demo3(self):
        x = "hello world"
        print(f"{x} python")
        assert 'h' in x

    def test_demo2(self):
        x = 'hello'
        assert hasattr(x,"check")


if __name__ == "__main__":
    pytest.main(['-v', '-s','test_2.py'])

看着默认执行顺序是代码中函数的顺序

更改用例的执行顺序,可以安装包 pip install pytest-ordering

按序执行@pytest.mark.run(order=x)

import sys

import pytest

class TestDemo:
    @pytest.mark.run(order=1)
    def test_demo1(self):
        x = "hello world"
        print(f"{x} python")
        assert 'h' in x

    @pytest.mark.run(order=3)
    def test_demo3(self):
        x = "hello world"
        print(f"{x} python")
        assert 'h' in x

    @pytest.mark.run(order=2)
    def test_demo2(self):
        x = 'hello'
        assert hasattr(x,"check")


if __name__ == "__main__":
    pytest.main(['-v', '-s','test_2.py'])

依赖执行

pip install pytest-dependency

用例默认执行顺序是test1-->test2-->test3,test2依赖test1,test3依赖test2。

test1通过才会执行test2,test2通过才会执行test3,如果test1失败,则后续依赖的用例跳过,

如果依赖的用例还未执行,也会跳过

import sys

import pytest

class TestDemo:
    @pytest.mark.dependency(name="test1")
    def test_demo1(self):
        x = "hello world"
        print(f"{x} python")
        assert 'h' in x

    @pytest.mark.dependency(name="test3",depends=['test2'])
    def test_demo3(self):
        x = "hello world"
        print(f"{x} python")
        assert 'h' in x

    @pytest.mark.dependency(name="test2",depends=['test1'])
    def test_demo2(self):
        x = 'hello'
        assert hasattr(x,"check")


if __name__ == "__main__":
    pytest.main(['-v', '-s','test_2.py'])

test_demo2未执行,test_demo3跳过

import sys

import pytest

class TestDemo:
    @pytest.mark.dependency(name="test1")
    @pytest.mark.run(order=1)
    def test_demo1(self):
        x = "hello world"
        print(f"{x} python")
        assert 'h' in x

    @pytest.mark.dependency(name="test3",depends=['test2'])
    @pytest.mark.run(order=3)
    def test_demo3(self):
        x = "hello world"
        print(f"{x} python")
        assert 'h' in x

    @pytest.mark.dependency(name="test2",depends=['test1'])
    @pytest.mark.run(order=2)
    def test_demo2(self):
        x = 'hello'
        assert hasattr(x,"check")


if __name__ == "__main__":
    pytest.main(['-v', '-s','test_2.py'])

顺序对了,但是test_demo2失败,test_demo3跳过

import sys

import pytest

class TestDemo:
    @pytest.mark.dependency(name="test1")
    @pytest.mark.run(order=1)
    def test_demo1(self):
        x = "hello world"
        print(f"{x} python")
        assert 'h' in x

    @pytest.mark.dependency(name="test3",depends=['test2'])
    @pytest.mark.run(order=3)
    def test_demo3(self):
        x = "hello world"
        print(f"{x} python")
        assert 'h' in x

    @pytest.mark.dependency(name="test2",depends=['test1'])
    @pytest.mark.run(order=2)
    def test_demo2(self):
        x = 'hello'
        assert hasattr(x,"replace")


if __name__ == "__main__":
    pytest.main(['-v', '-s','test_2.py'])

顺序对,依赖对

相关推荐
赵钰老师1 小时前
【ArcGIS】基于R语言、MaxEnt模型融合技术的物种分布模拟、参数优化方法、结果分析制图与论文写作
开发语言·arcgis·数据分析·r语言
xiao5kou4chang6kai43 小时前
Python和OpenCV支持下的空天地遥感数据识别与计算(涵盖土壤成分分析、农作物分类、森林火灾检测、水体动态监测等)
python·opencv·chatgpt·遥感·农业分析·生态评估·地质监测
小团团04 小时前
Python中主要控制结构的详细说明
开发语言·python
Dxy12393102164 小时前
python制造一个报错
开发语言·python
流星白龙4 小时前
【C++算法】41模拟_外观数列
开发语言·c++·算法
黑金IT5 小时前
编程语言对决:Node.js与Python在视频处理领域
python·node.js·音视频
小丸子灬5 小时前
clipboard----封装复制组件
开发语言·javascript
小萌新~~~~6 小时前
在Scala中正则表达式的类型
开发语言·正则表达式·scala
洋芋爱吃芋头6 小时前
scala的泛型2
开发语言·后端·scala