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

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

相关推荐
2601_9623878210 小时前
web自动化测试实战教程【selenium/unittest/pytest】【共193课
selenium·pytest·devops·web自动化测试·unittest
weixin_4407305012 小时前
使用pytest中方法控制执行步骤(test_begin.py、test_end.py,@pytest.mark.run(order=1))
开发语言·python·pytest
2601_9622972513 小时前
Python、Pytest、Allure、Selenium和Jenkins实现自动化测试集成实例
python·selenium·jenkins·pytest·allure
2601_962077602 天前
从零搭建Python接口自动化测试框架:pytest+requests实战指南
python·pytest·接口自动化·requests·框架搭建
weixin_440730502 天前
pytest结合allure生成html测试报告(step、story、severity、screenshot)
前端·html·pytest·allure报告
拜托多多指教2 天前
【Pytest框架学习】分层架构
python·pytest
2601_962299882 天前
python脚本如何单元测试
python·单元测试·pytest·unittest·mock对象
Tizzy JJ3 天前
Python + pytest 接口自动化测试框架实战:从零搭建企业级项目骨架
开发语言·python·pytest
Tizzy JJ3 天前
接口自动化测试实战:如何围绕真实业务场景设计高质量用例
python·自动化·pytest
测试老哥4 天前
Pytest 之assert断言的使用
自动化测试·软件测试·python·测试工具·测试用例·pytest·接口测试