python - 在 Pytest 中,根级别的 conftest.py 是否会被树中更深处的 conftest 覆盖?

标签 python selenium automation pytest

我们当前的项目有一个项目树,在测试的根级别有一个conftest.py,如下所示:

Tests
   test_folder_1
   test_folder_2
   conftest.py

这个根级别conftest.py导入我们在 session 和类(class)级别的大部分固定装置。但在 test_folder_1 中,我们有另一个 conftest.py,我们想要覆盖其中一些类级别固定装置并将它们转变为函数范围。

我的印象是这会起作用并且一直在起作用,但现在我遇到了其中一些错误:您尝试使用“class”访问“function”作用域固定装置“new_primary_contact”作用域请求对象

如果这不是从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

我只是想覆盖某些固定装置,因此我的功能级别测试不会使用类范围。

这是在 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.'

测试在登录之前和网络驱动程序/浏览器启动之后失败(名称已更改以保护代码)

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)

最佳答案

只有当我 100% 确定所有测试都使用那里的所有导入装置时,我才会将 conftest.py 添加到 ROOT 文件夹,否则您只会减慢自动化执行速度,这一点至关重要。

我已经测试了以下内容:

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 中更新的固定装置。 由于范围发生了变化,我无法将此固定装置应用于类。

关于python - 在 Pytest 中,根级别的 conftest.py 是否会被树中更深处的 conftest 覆盖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68866409/

相关文章:

python - Flask Json 请求 400 记录?

python - 无法使用 python 和 dogtail 自动执行远程程序,远程服务器中没有图形界面......

c# - 如何使用自动化脚本在一天中的不同时间测试我的 Web 应用程序的性能?

python - 在 R 中增加向量或矩阵的一维

python - Pandas :删除连续的重复项但保留第一个和最后一个值

javascript - 使用 Selenium Driver + PhantomJS 注入(inject) JavaScript 脚本并在 Python 中正确处理重定向

java - 扩展 PageFactory 注释以支持 wait4element(By)?

java - Selenium Java如何让驱动返回到上一页

node.js - testcafe 正在打开浏览器并默认为/browser/connect/.... 并且无法执行测试

python - 如何通过检查条件来替换数据框中的值?