pytest

pytest AttributeError when using fixture with yield

戏子无情 提交于 2021-01-08 15:28:32
问题 I am using pytest fixture with yield. But receive AttributeError when trying to get value that yield returns conftest.py @pytest.fixture() def driver_setup(): driver = webdriver.Firefox() yield driver driver.quit() basetest.py @pytest.mark.usefixtures("driver_setup") class BaseTest: pass test_example.py class TestExample(BaseTest): def test_example(self): self.driver.get(url) pass Output: AttributeError: 'TestExample' object has no attribute 'driver' 回答1: You need to update driver_setup

How can request.param be annotated in indirect parametrization?

痞子三分冷 提交于 2021-01-07 06:57:05
问题 In the Indirect parametrization example I want to type hint request.param indicating a specific type, a str for example. The problem is since the argument to fixt must be the request fixture there seems to be no way to indicate what type the parameters passed through the "optional param attribute" should be (quoting the documentation). What are the alternatives? Perhaps documenting the type hint in the fixt docstring, or in the test_indirect docstring? @pytest.fixture def fixt(request):

How can I access al the markers in a pytest fixture?

社会主义新天地 提交于 2021-01-07 04:34:25
问题 I'm using pytest and I want to tag my tests with markers which will specify to a fixture which page to load in my driver. This works easily with the behave context object, but I cannot find how to do it with pytest. For this code for example import pytest @pytest.fixture def text(request): if 'hello' in X: return 'found it!' return 'did not find it :(' @pytest.mark.hello def test_hello(text): assert text == 'found it!' what should X be so that I can pass this test? I tried request.node.own

手把手教你搭建Pytest+Allure2.X环境详细教程,生成让你一见钟情的测试报告

无人久伴 提交于 2021-01-07 02:31:51
简介 宏哥之前在做接口自动化的时候,用的测试报告是HTMLTestRunner,虽说自定义模板后能满足基本诉求,但是仍显得不够档次,高端,大气,遂想用其他优秀的report框架替换之。一次偶然的机会,在一个QQ群里看到Allure的测试报告,真的是一见钟情,特别的喜欢。但是由于时间的原因就没有自己实践一下,乘着国庆假期,自己特抽时间做了一番探索。 Allure介绍 Allure是一种灵活的轻量级多语言测试报告工具,它不仅可以以简洁的Web报告形式非常简洁地显示已测试的内容,而且还允许参与开发过程的每个人从日常执行中提取最大程度的有用信息。 从开发/质量保证的角度来看,Allure报告可以缩短常见缺陷的生命周期:可以将测试失败划分为bug和残破的测试,还可以配置日志,步骤,固定装置,附件,时间,历史记录以及与TMS的集成以及Bug跟踪系统,因此负责任的开发人员和测试人员将掌握所有信息。 从管理者的角度来看,Allure提供了一个清晰的“全局”,涵盖了所涵盖的功能,缺陷聚集的位置,执行时间表的外观以及许多其他方便的事情。 魅力的模块化和可扩展性确保您始终可以微调某些东西,以使魅力更适合您。 一睹Allure风采 在展开Allure详述前,先上一份测试报告,报告主要包含总览、类别、测试套件、图表、时间刻度、功能、包等7大部分,支持自定义诸多信息,包括附件添加、缺陷链接、案例链接、测试步骤

Calling function that yields from a pytest fixture

旧街凉风 提交于 2021-01-05 11:21:20
问题 In my unit tests, I have two very similar fixtures, and I was hoping to break out some of the functionality into a helper function of some kind. Given my understanding of how yield produces generators, I don't feel like this should cause any kind of problem. my_fixture_with_helper , should just return the generator that `fixture_helper produces. import pytest def fixture_helper(): print("Initialized from the helper...") yield 26 print("Tearing down after the helper...") @pytest.fixture def my

Calling function that yields from a pytest fixture

时间秒杀一切 提交于 2021-01-05 11:18:45
问题 In my unit tests, I have two very similar fixtures, and I was hoping to break out some of the functionality into a helper function of some kind. Given my understanding of how yield produces generators, I don't feel like this should cause any kind of problem. my_fixture_with_helper , should just return the generator that `fixture_helper produces. import pytest def fixture_helper(): print("Initialized from the helper...") yield 26 print("Tearing down after the helper...") @pytest.fixture def my

Why a Fatal Python error when testing using pytest-qt?

五迷三道 提交于 2021-01-05 08:53:16
问题 My first test using pytest-qt failed immediately with a Fatal Python error. I reduced the code to this (a test that would never pass, but shouldn't crash): from PyQt5 import QtCore as qtc class sut(qtc.QObject): sig_sig_sputnik = qtc.pyqtSignal() def __init__(self): super().__init__() def listen(self): pass def test_emit(qtbot): uut = sut() with qtbot.waitSignal(sut.sig_sig_sputnik, raising=True): uut.listen() Executing this fails with: === test session starts ==== platform linux -- Python 3

How to run a pytest test for each item in a list of arguments

时光毁灭记忆、已成空白 提交于 2021-01-05 07:18:26
问题 Suppose I have a list of HTTP URLs like endpoints = ["e_1", "e_2", ..., "e_n"] And I want to run n tests, one for each endpoint. How can I do that? A simple way to test all the endpoints at once would be def test_urls(): for e in endpoints: r = get(e) assert r.status_code < 400 or something like that. But as you can see, this is one test for all the n endpoints and I would like a little more granularity than that. I have tried using a fixture like @fixture def endpoint(): for e in endpoints:

How to run a pytest test for each item in a list of arguments

回眸只為那壹抹淺笑 提交于 2021-01-05 07:17:06
问题 Suppose I have a list of HTTP URLs like endpoints = ["e_1", "e_2", ..., "e_n"] And I want to run n tests, one for each endpoint. How can I do that? A simple way to test all the endpoints at once would be def test_urls(): for e in endpoints: r = get(e) assert r.status_code < 400 or something like that. But as you can see, this is one test for all the n endpoints and I would like a little more granularity than that. I have tried using a fixture like @fixture def endpoint(): for e in endpoints:

pytest reports too much on assert failures

别等时光非礼了梦想. 提交于 2021-01-04 07:47:05
问题 Is there a way for pytest to only output a single line assert errors? This problem arises when you have modules with asserts, If those asserts fails, it dumps the entire function that failed the asserts. > assert r.status_code == 200, f"{idtest.tools.now()} wrong status code {r.status_code}: resp:{r.text}" E AssertionError: 2019-06-11 12:41:17.239128 wrong status code 500: resp:{"timestamp":"2019-06-11T10:41:17.187+0000","status":500,"error":"Internal Server Error","message":"The user was not