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

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

相关推荐
努力搬砖的咸鱼2 天前
从零开始搭建 Pytest 测试框架(Python 3.8 + PyCharm 版)
python·pycharm·pytest
FINE!(正在努力!)4 天前
PyTest框架学习
学习·pytest
程序员杰哥4 天前
接口自动化测试之pytest 运行方式及前置后置封装
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
测试老哥4 天前
Pytest+Selenium UI自动化测试实战实例
自动化测试·软件测试·python·selenium·测试工具·ui·pytest
水银嘻嘻5 天前
07 APP 自动化- appium+pytest+allure框架封装
python·appium·自动化·pytest
天才测试猿5 天前
接口自动化测试之pytest接口关联框架封装
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
not coder6 天前
Pytest Fixture 详解
数据库·pytest
not coder6 天前
pytest 常见问题解答 (FAQ)
开发语言·python·pytest
程序员的世界你不懂6 天前
(1)pytest简介和环境准备
pytest
not coder6 天前
Pytest Fixture 是什么?
数据库·oracle·pytest