在 Pytest 中,根级别的 conftest.py 是否会被树中更深层的冲突覆盖?
In Pytest, does conftest.py at the root level get overridden by conftests deeper in the tree?
我们当前的项目有一个项目树,在测试的根级别有一个 conftest.py,如下所示:
Tests
test_folder_1
test_folder_2
conftest.py
这个根级别 conftest.py 在会话和 class 级别导入我们的大部分装置。但是在 test_folder_1 中,我们有另一个 conftest.py 我们想要覆盖其中的一些 class 级别fixtures 并将它们变成功能范围。
我的印象是这会起作用并且一直在起作用,但现在我遇到了其中一些错误:You tried to access the 'function' scoped fixture 'new_primary_contact' with a 'class' scoped request object
如果这不是覆盖 conftest.py 中的固定装置的方法,还有其他方法吗?这可能是另一个错误,但我真的很想知道树中的 conftest.py 是否真的覆盖了高级错误。感谢您的帮助!
编辑:
这是我的代码的一个最小示例:
根级别conftest.py:
from automation.Fixtures.ClassScope import config, api_client, new_related_contact, \
new_primary_contact, auto_clean_contacts_helper as contacts_helper, create_contact_payload, document_helper
在我较低级别的测试中,在 root/feature_1 中,我在 conftest.py 中有类似的内容:
from automation.Fixtures.FunctionScope import new_related_contact, \
new_primary_contact, auto_clean_contacts_helper as contacts_helper, create_contact_payload, document_helper
我只是想覆盖某些固定装置,所以我的功能级别测试不会使用 class 范围。
这是在 root/feature_1/Tests:
中使用它们的测试示例
class TestDeleteRelatedContactWorkflow:
def test__remove_related_contact(self, authenticated_chrome_driver, config, new_primary_contact, new_related_contact, new_contact_proposal, contacts_helper):
primary_contact_name = new_primary_contact["display_name"]
related_contact_name = new_related_contact["display_name"]
related_contact_id = new_related_contact["ContactId"]
document_id = new_contact_document["id"]
web_nav = WebNavigation(authenticated_chrome_driver, config)
web_nav.navigate_to_document_view_page(document_id)
...
response = contacts_helper.delete_contact(related_contact_id)
assert response["IsSuccess"] is True, f'Deletion of contact "{related_contact_name}" not successful.'
assert response["StatusCode"] == 200, 'Status code should == 200 when deleting contact.'
测试在登录前和 webdriver/browser 启动后失败(更改名称以保护代码)
test setup failed
ScopeMismatch: You tried to access the 'function' scoped fixture 'new_primary_contact' with a 'class' scoped request object, involved factories
root/Fixtures/ClassScope/class_scoped_document_fixtures.py:19: def new_document_payload(config, new_primary_contact)
root/Fixtures/FunctionScope/function_scoped_contacts_fixtures.py:85: def new_primary_contact(contacts_helper, create_contact_payload)
我会将 conftest.py 添加到 ROOT 文件夹,前提是我 100% 确定我的所有测试都使用那里所有导入的固定装置,否则你只是在减慢自动化执行速度,这很关键。
我测试了以下内容:
Tests
conftest.py
test_folder_1
conftest.py
test_folder_2
两个 conftest.py 文件都有同名的 fixture 函数。
在 test_folder_1/conftest.py - 我设置了 scope="function" 并返回了与 Tests/conftest.py 中的相同夹具相比不同的值(它有 scope="class").
在test_folder_1下的测试中,
我能够使用来自 test_folder_1/conftest.py 的更新夹具。
由于范围已更改,我无法在 class 上应用此固定装置。
我们当前的项目有一个项目树,在测试的根级别有一个 conftest.py,如下所示:
Tests
test_folder_1
test_folder_2
conftest.py
这个根级别 conftest.py 在会话和 class 级别导入我们的大部分装置。但是在 test_folder_1 中,我们有另一个 conftest.py 我们想要覆盖其中的一些 class 级别fixtures 并将它们变成功能范围。
我的印象是这会起作用并且一直在起作用,但现在我遇到了其中一些错误:You tried to access the 'function' scoped fixture 'new_primary_contact' with a 'class' scoped request object
如果这不是覆盖 conftest.py 中的固定装置的方法,还有其他方法吗?这可能是另一个错误,但我真的很想知道树中的 conftest.py 是否真的覆盖了高级错误。感谢您的帮助!
编辑: 这是我的代码的一个最小示例:
根级别conftest.py:
from automation.Fixtures.ClassScope import config, api_client, new_related_contact, \
new_primary_contact, auto_clean_contacts_helper as contacts_helper, create_contact_payload, document_helper
在我较低级别的测试中,在 root/feature_1 中,我在 conftest.py 中有类似的内容:
from automation.Fixtures.FunctionScope import new_related_contact, \
new_primary_contact, auto_clean_contacts_helper as contacts_helper, create_contact_payload, document_helper
我只是想覆盖某些固定装置,所以我的功能级别测试不会使用 class 范围。
这是在 root/feature_1/Tests:
中使用它们的测试示例class TestDeleteRelatedContactWorkflow:
def test__remove_related_contact(self, authenticated_chrome_driver, config, new_primary_contact, new_related_contact, new_contact_proposal, contacts_helper):
primary_contact_name = new_primary_contact["display_name"]
related_contact_name = new_related_contact["display_name"]
related_contact_id = new_related_contact["ContactId"]
document_id = new_contact_document["id"]
web_nav = WebNavigation(authenticated_chrome_driver, config)
web_nav.navigate_to_document_view_page(document_id)
...
response = contacts_helper.delete_contact(related_contact_id)
assert response["IsSuccess"] is True, f'Deletion of contact "{related_contact_name}" not successful.'
assert response["StatusCode"] == 200, 'Status code should == 200 when deleting contact.'
测试在登录前和 webdriver/browser 启动后失败(更改名称以保护代码)
test setup failed
ScopeMismatch: You tried to access the 'function' scoped fixture 'new_primary_contact' with a 'class' scoped request object, involved factories
root/Fixtures/ClassScope/class_scoped_document_fixtures.py:19: def new_document_payload(config, new_primary_contact)
root/Fixtures/FunctionScope/function_scoped_contacts_fixtures.py:85: def new_primary_contact(contacts_helper, create_contact_payload)
我会将 conftest.py 添加到 ROOT 文件夹,前提是我 100% 确定我的所有测试都使用那里所有导入的固定装置,否则你只是在减慢自动化执行速度,这很关键。
我测试了以下内容:
Tests
conftest.py
test_folder_1
conftest.py
test_folder_2
两个 conftest.py 文件都有同名的 fixture 函数。 在 test_folder_1/conftest.py - 我设置了 scope="function" 并返回了与 Tests/conftest.py 中的相同夹具相比不同的值(它有 scope="class").
在test_folder_1下的测试中, 我能够使用来自 test_folder_1/conftest.py 的更新夹具。 由于范围已更改,我无法在 class 上应用此固定装置。