pytest

Why would a pytest factory as fixture be used over a factory function?

不羁的心 提交于 2019-12-07 17:55:25
问题 In the py.test docs it describes declaring factory methods as fixtures, like-so: @pytest.fixture def make_foo(): def __make_foo(name): foo = Foo() foo.name = name return foo return __make_foo What are the benefits/tradeoffs of doing this over just defining a make_foo function and using that? I don't understand why it is a fixture. 回答1: Actually, the most important advantage is being able to use other fixtures, and make the dependency injection of pytest work for you. The other advantage is

Pytest - python testing with asyncio

元气小坏坏 提交于 2019-12-07 16:39:23
问题 Is it possible to return execution to event loop from a function. And as soon Task will be completed return to the function and continue execution? Im trying to use pytest-asyncio plugin Example: @pytest.mark.asyncio async def test_async1(event_loop): print('start 1') res = event_loop.create_task( send_async_request("http://test.com", limit=1000))) # here I need to return execution to event loop and continue only after getting response from send_async_request function print('end1',res)

Issue mocking with a decorated method call in Python 2.7

戏子无情 提交于 2019-12-07 12:46:18
问题 This code: import mock from functools import wraps def dec(f): @wraps(f) def f_2(*args, **kwargs): pass return f_2 class Example(object): def __init__(self): pass @dec def method_1(self, arg): pass def method_2(self, arg): self.method_1(arg) def test_example(): m = mock.create_autospec(Example) Example.method_2(m, "hello") m.method_1.assert_called_once_with("hello") produces this error with py.test def test_example(): m = mock.create_autospec(Example) > Example.method_2(m, "hello") example.py

If I have multiple tests in one class and a preceding test fails, how do I have it skip or exit the class instead of testing the remaining tests?

限于喜欢 提交于 2019-12-07 11:57:15
问题 I'm using Python with Selenium and unittest. I have four tests in one class since they're all related unit tests. How do I have it skip the next test(s) if the preceding test fails? I've read up on all the documentation for unittest's skip methods, but none of it is exactly what I need. Is there a way to tell it to exit the class? Here is a jist of what my code currently looks like: def test_dealer_search_id_contains(self): try: LoginPage.login(self, ULV.AA_USERNAME, ULV.AA_PASSWORD) except

How to add additional variable to pytest html report

强颜欢笑 提交于 2019-12-07 11:52:24
问题 I am using pytest HTML report plugin for my selenium tests. It works great with just passing test.py --html==report.htmlin command line and generates a great report. I also need to implement additional string/variable to each test case display. It doesn't matter if it's pass or failed it should just show "ticket number". This ticket id I can return in each test scenario. I could just add ticket number to test name, but it will look ugly. Please advise what's the best way to do it. Thank you.

Force py.test to use installed version of module

喜你入骨 提交于 2019-12-07 11:33:41
问题 I have a mixed Python/C++ library with test files mixed in amongst source files in the same directories. The layout looks like /home/irving/geode geode __init__.py vector __init__.py test_vector.py ... ... Unfortunately, the library is unusable in-place since it lacks .so extension modules. Question : Can I make py.test always use an installed version, even when run from /home/irving/geode or a subdirectory? The test files have from __future__ import absolute_import , and run fine if executed

Dynamically control order of tests with pytest

…衆ロ難τιáo~ 提交于 2019-12-07 11:27:32
问题 I would like to control the order of my tests using logic which will reorder them on the fly, while they are already running. My use case is this: I am parallelizing my tests with xdist, and each test uses external resources from a common and limited pool. Some tests use more resources than others, so at any given time when only a fraction of the resources are available, some of the tests have the resources they need to run and others don't. I want to optimize the usage of the resources, so I

Fail remaining pytest tests if a specific one fails

自作多情 提交于 2019-12-07 07:40:05
问题 So I have a directory filled with a bunch of tests written in python with the proper syntax to make sure they run in order. So let's say I have a test which if fails, currently calls pytest.exit('Exit Message'). The problem with this is that the generated test output in XML only records the tests preceding it. I would prefer that the whole suite is run but are reported as failed if the test mentioned above fails. A solution I thought of was setting an environment variable in case it fails and

How send a config variable to a py.test test?

◇◆丶佛笑我妖孽 提交于 2019-12-07 07:01:18
问题 I have a test suite which needs to run with multiple backends. It isn't a simple parameterized test though since it applies to the whole suite (multiple files/modules). I can control the run via the environment, but I'm wondering if py.test has a clearer way to express this. That is, I'm looking for something like this: py.test --set-mode ALPHA Then in my test I would read this value: if py.test.mode == 'ALPHA': 回答1: Using pytest_addoption: test_blah.py def test_something(mode): if mode ==

Run a test with two different pytest fixtures

大憨熊 提交于 2019-12-07 06:34:53
问题 I am currently testing a web app using pytest and Selenium. All pages have "Home" and "Log Out" links, so I have written a test like this: def test_can_log_out(page): link = page.find_element_by_partial_link_text('Log Out') link.click() assert 'YOU HAVE SUCCESSFULLY LOGGED OFF!' in starting_page.page_source Now for the page fixture, I am simulating the login process. I have this broken into several fixtures: Get the Selenium WebDriver instances @pytest.fixture() def browser(request, data,