【PYTEST】第一章常用命令

pytest入门

  • 安装pytest
  • 运行pytest
  • pytest常用命令

1. 安装pytest

pip install pytest

2. 运行pytest

2.1 pytest默认搜索当前目录下以test_, _test结尾的测试函数(默认是以test开头,结尾,但是这个是可以自己配置的,后面再说)

pytest/ch1/test_one.py

def test_1():
    assert (1, 2, 3) == (1, 2, 3)

def test_2():
    assert 1 == 2

运行指令
$cd /pytest/ch1
$pytest

2.2 运行单个测试函数

$ cd /pytest/ch1
$ pytest test_one.py

2.3 运行单个测试用例

$ cd /pytest/ch1
$ pytest test_one.py
$ pytest test_one.py::test_1

3. 常用的命令

3.1  -v 查看执行结果的详细信息

3.2 -q 简化输出

3.3 -l  显示错误堆栈里的局部变量

3.4 -m 标记测试分组,用@pytest.mark.xxx装饰器做标记

pytest/ch1/test_two.py

import pytest

@pytest.mark.smoke
def test_pass():
    assert (1, 2, 3) == (1, 2, 3)


def test_fail():
    assert 1 == 2

$ cd /pytest/ch1
$ pytest -m smoke test_two.py

观察结果,只运行了标记为smoke的测试用例

原文地址:https://www.cnblogs.com/totoro-cat/p/13372279.html