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要进行区分

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

相关推荐
lucia_zl2 天前
pytest并发测试,资源问题导致用例失败解决办法
pytest
鱼鱼说测试2 天前
Selenium4+Pytest自动化测试框架实战
pytest
XYiFfang8 天前
【Pytest】解决Pytest中Teardown钩子的TypeError:实例方法与类方法的调用差异
python·pytest
Kingairy10 天前
Pytest 插件:pytest_runtest_protocol
python·pytest
AIZHINAN10 天前
Pytest 插件介绍和开发
测试工具·pytest·插件开发
灰阳阳12 天前
替身演员的艺术:pytest-mock 从入门到飙戏
自动化测试·python·pytest·unit testing·pytest-mock
年年测试16 天前
Playwright与PyTest结合指南
pytest
focksorCr16 天前
pytest 并发执行用例(基于受限的测试资源)
python·pytest
律品21 天前
pytest的前置与后置
开发语言·python·pytest