在不同级别上重写fixture
在大型的test suite中,可能需要重写一个全局的fixture。
在文件夹层级上重写fixture(conftest)
Example:
目录结构
目录结构
在tests/conftest.py的内容为
tests/conftest.py
在tests/test_name.py的内容为
tests/test_name.py
tests/newfunction/conftest.py
tests/newfunction/conftest.py
tests/newfunction/test_newfunctionname.py
tests/newfunction/test_newfunctionname.py
执行结果
结果
由上例可以看出,在newfunction目录下的conftest.py中的fixture重写了tests目录下conftest.py中的fixture
用module级别的fixture重写conftest.py中的fixture
Example:
目录结构
目录结构
在tests/conftest.py中的fixture为
conftest.py
test_name module的定义
test_name.py
运行结果
运行结果
由结果可以看出 test_name.py模块中的同名的fixture替代了conftest.py中的fixture
使用直接测试参数覆盖fixture
参数化方法时,参数化的参数名称与fixture的名字一样是,fixture将被参数化覆盖
Example
参数名和fixture相同,fixture被覆盖
结果
结果
fixture的参数名和参数化的参数名相同时,参数化的参数会被传入到fixture中
Example
fixture
结果
有参的fixture可以覆盖无参的fixture,反之亦然
有参的fixture覆盖无参的fixture
Example
feature
scenario
结果
无参的fixture覆盖有参的fixture
Example
feature
scenario
Result