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

2024-05-18 12:04:37 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tests
   test_folder_1
   test_folder_2
   conftest.py

这个根级别conftest.py在会话和类级别导入我们的大多数装置。但是在test\u文件夹\u 1中,我们还有另一个conftest.py,我们希望覆盖一些类级装置,并将其转换为函数范围

我当时的印象是,这会起作用,而且一直在起作用,但现在我发现其中一些错误: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

在我的低级测试中,在根/特征_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

我只是想覆盖某些装置,所以我的函数级测试不会使用类范围

下面是一个在根/特征\u 1/测试中使用它们的测试示例:

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/浏览器启动之后失败(名称更改为保护代码)

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)

Tags: namepytesthelperidnewcontact级别
1条回答
网友
1楼 · 发布于 2024-05-18 12:04:37

我会将conftest.py添加到根文件夹,前提是我100%确定我的所有测试都使用了那里所有导入的fixture,否则您只会降低自动化执行的速度,这是至关重要的

我已测试了以下各项:

Tests
   conftest.py
   test_folder_1
        conftest.py
   test_folder_2

两个conftest.py文件都具有同名的fixture函数。 在test_文件夹_1/conftest.py中,我设置了scope=“function”,并返回了与Tests/conftest.py中相同夹具(具有scope=“class”)不同的值

在test_文件夹_1下的测试中, 我能够使用test_文件夹_1/conftest.py中更新的夹具。 由于作用域发生了变化,我无法将此固定装置应用于类

相关问题 更多 >