用python的pytest完成: f1为fixture函数,传入两个参数,test_2函数调用f1,传入参数为1,2
时间: 2024-04-09 07:33:26 浏览: 162
pytest1:python项目
Sure! Here's an example of how you can use pytest in Python to accomplish this:
```python
import pytest
@pytest.fixture
def f1():
def _f1(a, b):
return a + b
return _f1
def test_2(f1):
result = f1(1, 2)
assert result == 3
```
In the above code, we define a fixture function `f1` using the `@pytest.fixture` decorator. This fixture function takes two arguments `a` and `b` and returns their sum.
The `test_2` function is a pytest test function that utilizes the `f1` fixture. It calls `f1` with arguments 1 and 2 and asserts that the result is equal to 3.
To run this test using pytest, save the code in a file (e.g., `test_example.py`) and run the following command in the terminal:
```
pytest test_example.py
```
I hope this helps! Let me know if you have any more questions.
阅读全文