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'])

顺序对,依赖对

相关推荐
雾月552 小时前
LeetCode 941 有效的山脉数组
java·开发语言·数据结构·算法·leetcode·职场和发展
晨曦5432103 小时前
函数和模式化——python
开发语言·python
leluckys3 小时前
swift-08-属性、汇编分析inout本质
开发语言·汇编·swift
Verdure陌矣5 小时前
游戏开发中 C#、Python 和 C++ 的比较
c++·python·游戏·c#
嘤国大力士5 小时前
C++11&QT复习 (十六)
java·开发语言·c++
丰锋ff5 小时前
借助 AI 工具使用 Python 实现北京市店铺分布地理信息可视化教程
人工智能·python·信息可视化
念九_ysl5 小时前
Java中的列表(List):操作与实现详解
java·开发语言·list
阿昆的科研日常5 小时前
Matlab进阶绘图第74期-带填充纹理的单组柱状图
开发语言·matlab·可视化·论文插图