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 - 在 python 中编写类时,首选 mixins 还是对象属性?

java - 从 IDE 生成的基本 Selenium Java 不工作

java - 如何使用Java下载 protected 网页

python - 如何在 Keras 中调整(插值)张量?

python - 从 scipy.linalg 导入 _fblas : ImportError: DLL load failed: The specified module could not be found

python - 在 Tweepy 中以可用形式 (JSON) 提取数据

node.js - 向 Selenium Webdriver 网格发出并行请求

xpath - 如何通过在xpath中输入文本来选择文本输入?

java - 我不希望我的函数返回 null。什么是最好的解决方案?

java - 在生产环境中自动化浏览器任务的最佳解决方案?