找一个写好的大型框架,里面包括pytest 的参数化,congftest 的使用,以及参数在各种scope的使用
时间: 2024-02-05 16:11:37 浏览: 16
一个常用的大型框架是pytest-bdd,它基于pytest,并且提供了BDD(行为驱动开发)的支持。在pytest-bdd中,可以通过参数化和fixture来实现参数在各种scope的使用。
下面是一个例子:
```python
# conftest.py
import pytest
@pytest.fixture(scope="session")
def db():
# setup
yield
# teardown
@pytest.fixture(scope="function")
def data():
return {"name": "John", "age": 30}
# test_file.py
import pytest
@pytest.mark.parametrize("input,expected", [("3+5", 8), ("2+4", 6)])
def test_eval(input, expected):
assert eval(input) == expected
def test_db(db):
# test database operations
pass
def test_data(data):
assert data["name"] == "John"
```
在上面的例子中,conftest.py中定义了两个fixture,一个是session级别的db,另一个是function级别的data。在test_file.py中,使用了@pytest.mark.parametrize来进行参数化测试,并且使用了db和data两个fixture进行测试。
在运行时,pytest会自动根据scope来执行fixture的setup和teardown操作。例如,db fixture只会在整个会话开始时执行一次setup操作,而data fixture在每个测试函数执行前都会执行一次setup操作。