Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pytest-将测试运行生成的数据提取到conftest中的文件/夹具中?_Python_Selenium_Automation_Pytest_Qa - Fatal编程技术网

Python Pytest-将测试运行生成的数据提取到conftest中的文件/夹具中?

Python Pytest-将测试运行生成的数据提取到conftest中的文件/夹具中?,python,selenium,automation,pytest,qa,Python,Selenium,Automation,Pytest,Qa,我正在使用Pytest框架测试web应用程序。 当测试运行时,它生成数据并将其存储到变量中 例如: @pytest.mark.usefixture("test_setup") class TestArticleCreation: def test_new_article_creation(self): art = ArticleCreation(self.driver) art.create_new_article() article_id

我正在使用Pytest框架测试web应用程序。 当测试运行时,它生成数据并将其存储到变量中

例如:

@pytest.mark.usefixture("test_setup")
class TestArticleCreation:
    def test_new_article_creation(self):
        art = ArticleCreation(self.driver)
        art.create_new_article()
        article_id = art.get_new_article_id()
        assert art.verify_article_created_in_db(article_id)
我想提取article_id,并将其存储起来,以便以后删除,但我不想让id提取过程成为测试本身的一部分,即使除了向测试中添加函数之外,我想不出/知道任何其他方法来实现这一点,这将把article_id值写入文件。Pytest是否为此提供了解决方案

我使用conftest.py来构建配置,如果有帮助的话,这个项目是用POM设计模式构建的


提前谢谢。

我找到了一个很好的解决方案,不确定是不是最好的。。 可以从模块级提取变量

因此,在TestClass上面,我声明了一个新变量type(list)。 当测试生成id时,我可以将其添加到上面的列表中

# tests/test_sanity.py
article_ids = []

@pytest.mark.usefixtures("test_setup")  # USE "test_setup"   FIXTURE with scope=class
class TestSanity:

    def test_global_var(self):
        art_id = '12321342154'
        global article_ids  # Declare the in module var as global
        article_ids.append(art_id)  # Add id generated in test run
        assert article_id == '12321342154'


# conftest.py

@pytest.mark.hookwrapper  # Add hook wrapper to tests teardown
def pytest_runtest_teardown(item):
    yield
    print(item.module.article_ids)  # By this access the global var decalred in our test module
我现在面临的唯一问题是称为每个测试而不是每个会话的钩子包装器

编辑:

找到了另一种方法:

@pytest.fixture(scope="session")
def ids():
    ids = []
    yield ids
    print(ids)


@pytest.fixture(scope="class")
def api_setup(request, ids):
    request.cls.ids = ids

@pytest.mark.usefixtures("api_setup") 
class TestSome:
    def test_something(self):
        global article_ids
        art_id = '123'
        self.ids.append(art_id)
        assert art_id == '123'

通过这种方式,它会将生成的每个id添加到列表中,但不会在每个测试结束时调用conftest.py

请添加更多有关您要测试的代码、用于web开发的框架以及您迄今为止所做的尝试的信息,以便我们可以。