pytest 框架常用函数
时间: 2023-09-05 13:10:20 浏览: 273
pytest是一个功能强大且易于使用的Python测试框架。以下是一些pytest框架中常用的函数:
1. `pytest.fixture`:用于定义测试用例执行前后需要进行的准备和清理工作的函数。可以将fixture函数作为参数传递给测试用例函数,以供测试用例使用。
2. `pytest.mark.parametrize`:用于生成一组参数化的测试用例。通过将参数列表传递给`@pytest.mark.parametrize`装饰器,可以为同一个测试用例定义多个输入参数和期望输出。
3. `pytest.skip`:用于跳过某个测试用例或整个测试模块。可以在测试用例函数或模块级别使用。
4. `pytest.xfail`:用于标记某个测试用例或整个测试模块为预期失败。如果测试用例失败了,会被标记为XFAIL,但不会报告为错误。
5. `pytest.raises`:用于验证代码是否引发了特定异常。可以使用`with pytest.raises(ExpectedException)`来捕获并断言代码是否引发了指定的异常。
6. `pytest.assert`系列函数:包括`assert xxx`、`assert xxx == yyy`等,用于编写断言语句。当断言失败时,会抛出`AssertionError`,指示测试失败。
7. `pytest.approx`:用于比较浮点数或复数。在比较时允许有一定的误差范围。
除了以上函数之外,pytest还提供了丰富的插件系统和自定义的扩展机制,可以进一步扩展其功能。
相关问题
pytest框架设计各个模块函数的指定执行顺序
pytest框架设计各个模块函数的指定执行顺序,可以通过使用装饰器 `@pytest.mark.run()` 来指定执行顺序。以下是几个常用的装饰器:
1. `@pytest.mark.run(order=n)`:指定测试函数执行的顺序,其中 `n` 是一个整数,表示执行的顺序,数字越小优先级越高。
```python
import pytest
@pytest.mark.run(order=1)
def test_function1():
pass
@pytest.mark.run(order=2)
def test_function2():
pass
@pytest.mark.run(order=3)
def test_function3():
pass
```
2. `@pytest.mark.dependency()`:指定测试函数之间的依赖关系,确保依赖的测试函数在被依赖的测试函数之前执行。
```python
import pytest
@pytest.mark.dependency()
def test_function1():
pass
@pytest.mark.dependency(depends=["test_function1"])
def test_function2():
pass
@pytest.mark.dependency(depends=["test_function2"])
def test_function3():
pass
```
3. `@pytest.mark.parametrize()`:参数化测试函数,并按照指定的参数执行多次。
```python
import pytest
@pytest.mark.parametrize("input", [1, 2, 3])
def test_function(input):
assert input > 0
```
这些装饰器可以根据你的需求来灵活组合使用,以满足不同的测试场景和执行顺序要求。
pytest框架mark
### pytest框架中的Mark用法
pytest提供了一种灵活的方式通过`mark`来标记测试函数,这使得可以对特定条件下的测试执行不同的操作。例如,在某些情况下可能只想运行带有特定标签的测试,或者跳过一些不适合当前环境配置的测试。
#### 基本语法
要给一个测试加上标志,可以在其定义前使用装饰器形式的应用程序接口(API)。最常用的内置marks包括`skip`, `skipif`, 和 `xfail`. 下面是一个简单的例子展示如何应用这些:
```python
import pytest
@pytest.mark.skip(reason="no way of currently testing this")
def test_the_unknown():
pass
```
对于更复杂的场景,比如基于条件决定是否应该跳过某个测试,则可利用`skipif`:
```python
import sys
import pytest
@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher")
def test_functionality_only_on_python_37_and_above():
assert True
```
当预期失败但仍希望记录下来以便后续审查时,`xfail`非常有用:
```python
import pytest
@pytest.mark.xfail(strict=True)
def test_feature_that_is_broken():
# This will be marked as expected to fail.
raise ValueError("This is a known issue.")
```
除了上述预设外,还可以创建自定义的marks用于分类目的或其他用途:
```python
import pytest
@pytest.mark.webtest
def test_example_website_response():
response = requests.get('http://example.com')
assert response.status_code == 200
```
为了确保所有使用的非标准marks都被注册到pytest环境中,可以在项目的根目录下创建名为`conftest.py`文件,并在里面声明它们:
```python
# content of conftest.py
import pytest
def pytest_configure(config):
config.addinivalue_line(
"markers", "webtest: mark tests that require web access."
)
```
这样做的好处是可以让其他开发者清楚哪些是特殊的测试类别,并且有助于防止拼写错误造成的误解。
最后值得注意的是,可以通过命令行参数指定只运行具有某种特性的测试案例。例如,如果想要仅限于那些被标记为`webtest`的测试,那么就可以这样做:
```bash
pytest -v -m webtest
```
此命令会告诉pytest只挑选含有`@pytest.mark.webtest`修饰符的方法来进行验证[^1]。
阅读全文
相关推荐
















