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

顺序对,依赖对

相关推荐
myshare20224 分钟前
Java开发提效秘籍:巧用Apache Commons IO工具库
java·开发语言
m0_748240254 小时前
【Python系列】Python 连接 PostgreSQL 数据库并查询数据
数据库·python·postgresql
柯南二号6 小时前
【Kotlin】上手学习之控制流程篇
android·开发语言·学习·kotlin
东躲西藏的西城6 小时前
重拾Python学习,先从把python删除开始。。。
python
skywalk81637 小时前
基于 Python 的财经数据接口库:AKShare
开发语言·python
白白糖7 小时前
深度学习 Pytorch 张量的索引、分片、合并以及维度调整
人工智能·pytorch·python·深度学习
白白糖7 小时前
深度学习 Pytorch 张量(Tensor)的创建和常用方法
人工智能·pytorch·python·深度学习
冰茶_7 小时前
C#中进程和线程的区别--17
开发语言·学习·c#
编程|诗人8 小时前
T-SQL语言的数据库交互
开发语言·后端·golang
27669582928 小时前
boss直聘 __zp_stoken__ 逆向分析
java·python·node.js·go·boss·boss直聘·__zp_stoken__