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

顺序对,依赖对

相关推荐
有风南来29 分钟前
算术图片验证码(四则运算)+selenium
自动化测试·python·selenium·算术图片验证码·四则运算验证码·加减乘除图片验证码
wangjinjin18030 分钟前
Python Excel 文件处理:openpyxl 与 pandas 库完全指南
开发语言·python
愚润求学30 分钟前
【C++】类型转换
开发语言·c++
斯奕sky_small-BAD42 分钟前
C++ if语句完全指南:从基础到工程实践
java·开发语言·php
Humbunklung1 小时前
Rust Floem UI 框架使用简介
开发语言·ui·rust
Yxh181377845541 小时前
抖去推--短视频矩阵系统源码开发
人工智能·python·矩阵
网安INF1 小时前
RSA加密算法:非对称密码学的基石
java·开发语言·密码学
明月*清风1 小时前
c++ —— 内存管理
开发语言·c++
蔡蓝2 小时前
设计模式-观察着模式
java·开发语言·设计模式
Humbunklung2 小时前
PySide6 GUI 学习笔记——常用类及控件使用方法(多行文本控件QTextEdit)
笔记·python·学习·pyqt