Pytest学习—执行用例常用参数

在用pytest执行用例时,可以按照以下方式来运行用例
1、执行目录及其子目录下的所有用例
pytest 文件名
2、执行某一个py文件下的用例

import pytest

def func(x):
    return x

def test_answer():

    assert func(5) == 5


def test_answer2():
    assert func(5) == 5

def test_answer3():
    assert func(5) == 5

Pytest学习—执行用例常用参数_第1张图片
3、-k 按关键字匹配
pytest test_class.py -k “TestClass and not two”
运行test_class.py中的TestClass.test_one,不运行TestClass.test_two。
Pytest学习—执行用例常用参数_第2张图片
4、按节点运行
每个收集的测试都分配了一个唯一的nodeid,由模块文件名后跟说明符组成。
运行文件中某个测试用例:
pytest test_sample.py::test_answer #文件名::函数名
Pytest学习—执行用例常用参数_第3张图片
运行文件中某个测试类中的某个用例:
pytest test_mod.py::TestClass::test_method
5、-m执行标记用例

import pytest
class TestClass(object):
    @pytest.mark.webtest
    def test_one(self):
        x = "this"
        assert 'h' in x

    def test_two(self):
        x = "hello"
        assert hasattr(x, 'check')

Pytest学习—执行用例常用参数_第4张图片
6、pytest -s
-s:执行用例,携带详细信息,比如打印的print内容.
Pytest学习—执行用例常用参数_第5张图片

7、-x 遇到错误时,停止测试
-x:遇到错误的用例,立即退出执行,并输出结果
Pytest学习—执行用例常用参数_第6张图片
8、用例错诨个数达到指定数量时,停止测试
pytest --maxfail=1 test_class.py
Pytest学习—执行用例常用参数_第7张图片

你可能感兴趣的:(Pytest,python,软件测试,pytest)