07、pytest指定要运行哪些用例

官方用例

shell 复制代码
# 目录结构
|
|----test_mod.py
|
|----testing
		|
		|----test_dir.py
python 复制代码
# content of test_mod.py
import pytest

def func(x):
    return x + 1

def test_mod():
    print("test_mod function was invoked")
    assert func(3) == 5
    
def test_func():
    print("test_func was invoked")
    assert 0
    
@pytest.mark.slow
def test_slow_func():
    print("test_slow_func was invoked.")
    assert 0
    
class TestMyClass:
    def test_something(self):
        print("TestMyClass.test_something was invoked")
        assert 0
        
    def test_method_simple(self):
        print("TestMyClass.test_method_simple was invoked")
        assert 0
        
class TestClass:
    def test_method(self):
        print("test_method was invoked.")
        assert 0
python 复制代码
# content of test_dir.py

def test_dir():
    print("test_dir function was invoked")
    assert 3 == 5

解读与实操

  • 只运行test_mod.py模块里面的测试用例


  • 运行testing目录及子目录下的测试用例
  • 运行文件名、类名、方法名中包含MyClass但不包含method的所有用例,例如TestMyClass.test_someting会运行,但TestMyClass.test_method_simple不会运行,Windows下用"",Linux或Mac下可使用''或""
  • 运行test_mod.py模块下的test_func函数
  • 运行test_mod.py模块下的TestClass类下的test_method函数
  • 运行被@pytest.mark.slow装饰的测试用例

场景应用

  1. 编写好了一个新的测试用例,想单独调试一下;
  2. 代码提测后,某个模块发生了改动,要调试一下;
  3. 生产环境与测试环境回归内容不同,根据mark要进行区分

以上场景,都可以通过指定要运行哪些用例来完成。

相关推荐
St_rive12 小时前
Pytest skip&&skipif跳过⽤例
运维·服务器·pytest
努力搬砖的咸鱼3 天前
AI Agent测试全景图:它到底改变了什么
人工智能·python·ai·集成测试·pytest·agent·ai编程
X1A0RAN5 天前
自动化流水线提效·微观篇:pytest 执行顺序优化
自动化·pytest
久久学姐12 天前
Python+Playwright+Pytest+BDD,用FSM打造高效测试框架
python·pytest·bdd·playwright·fsm
小白上线*^_^*12 天前
Pytest 自动化测试框架速通指南(二)
软件测试·pytest·python测试框架·ai测试基础
Logintern0913 天前
pytest的AST 断言重写
pytest
Tinyfacture14 天前
接口自动化之添加商品(pytest)
python·测试工具·自动化·pytest
ji_shuke16 天前
从零深入:基于 Playwright + Pytest + Allure 的企业级 Web 端到端自动化测试框架实战
前端·自动化测试·docker·jenkins·pytest·allure·playwright
祉猷并茂,雯华若锦18 天前
Selenium+Pytest自动化测试框架实战
selenium·测试工具·pytest
祉猷并茂,雯华若锦19 天前
Pytest+Selenium自动化测试:失败自动截图集成Allure报告
selenium·测试工具·pytest