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

顺序对,依赖对

相关推荐
微刻时光9 分钟前
影刀RPA开发-CSS选择器介绍
css·python·低代码·自动化·rpa·影刀rpa·影刀实战
水水沝淼㵘32 分钟前
嵌入式开发学习日志(数据结构--单链表)Day20
c语言·开发语言·数据结构·学习·算法
举一个梨子zz33 分钟前
Java—— 可变参数、集合工具类、集合嵌套、不可变集合
java·开发语言·intellij-idea·需求分析
iangyu37 分钟前
【windows server脚本每天从网络盘复制到本地】
开发语言·windows·php
程序员拂雨44 分钟前
Python知识框架
开发语言·python
灏瀚星空1 小时前
地磁-惯性-视觉融合制导系统设计:现代空战导航的抗干扰解决方案
图像处理·人工智能·python·深度学习·算法·机器学习·信息与通信
泽02021 小时前
C++类和对象之相关特性
java·开发语言·c++
Code_流苏1 小时前
《Python星球日记》 第72天:问答系统与信息检索
python·微调·问答系统·bert·应用场景·基于检索·基于生成
敲键盘的小夜猫1 小时前
深入理解Python逻辑判断、循环与推导式(附实战案例)
开发语言·python
Looooking1 小时前
Python 之 selenium 打开浏览器指定端口进行接续操作
python·selenium