pytest

How to run tests in django using database with data?

左心房为你撑大大i 提交于 2020-01-02 03:48:07
问题 I want to test my views using data from postgres localhost database (with already loaded data). I'm using tox with pytest and pytest-django. My question: How to set up / connect to local database to get all the data model schema and data itself? Or maybe it is better to use factory_boy? Or to load whole data from .sql script (if yes, how)? Example of my test: def test_foo_view(custom_client_login): response = custom_client_login.get('/foo/bar/123/') assert response.status_code == 200 assert

Why cant unittest.TestCases see my py.test fixtures?

心已入冬 提交于 2020-01-02 02:21:27
问题 I'm trying to use py.test 's fixtures with my unit tests, in conjunction with unittest . I've put several fixtures in a conftest.py file at the top level of the project (as described here), decorated them with @pytest.fixture , and put their names as arguments to the test functions that require them. The fixtures are registering correctly, as shown by py.test --fixtures test_stuff.py , but when I run py.test , I get NameError: global name 'my_fixture' is not defined . This appears to only

How do I get py.test to recognize conftest.py in a subdirectory?

若如初见. 提交于 2020-01-02 00:13:15
问题 So, I just lost a day trying to find out why py.test isn't executing my autouse, session-scoped setup and teardown fixtures. In the end I stumbled over (hat tip to this SO comment!) this little tidbit in the plugins documentation: Note that conftest.py files from sub directories are by default not loaded at tool startup. In my project, I got my py.test files ( conftest.py and tests files) in a tests/ subdirectory, which seems like a pretty standard setup. If I run py.test in the tests

How to customize the pytest name

蓝咒 提交于 2020-01-01 14:37:34
问题 I would like to customize the output name of my pytest to include the name of my fixtures So I have def test_t1( when_creating_a_project_from_a_sales_handoff, with_a_new_customer, and_no_conflicting_data_exists, create_project): it_will_create_a_customer_with_the_releavant_information() it_will_create_a_project_that_references_the_newly_created_customer() and I'd like the displayed test name to be some version of when_creating_a_project_from_a_sales_handoff with_a_new_customer and_no

How to run only unmarked tests in pytest

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-01 09:36:11
问题 I have several markers in my python test code: @pytest.mark.slowtest @pytest.mark.webtest @pytest.mark.stagingtest I am able to selectively run tests with a marker using for example pytest -m slowtest How can I run unmarked tests without resorting to pytest -m "not (slowtest or webtest or stagingtest)" ? As you can imagine, we might use other markers in the future... 回答1: You can use following code snippet in top level conftest.py. def pytest_runtest_setup(item): envmarker = item.get_marker()

pytest: How to pass a class parameter to setup_class

落花浮王杯 提交于 2020-01-01 09:24:11
问题 I am using pytest's parametrize annotations to pass params into a class. I'm able to use the parameters in the test methods, however, I can't figure out how to use the parameters in the setup_class method. import pytest params = ['A','B','C'] @pytest.mark.parametrize('n', params) class TestFoo: def setup_class(cls): print ("setup class:TestFoo") # Do some setup based on param def test_something(self, n): assert n != 'D' def test_something_else(self, n): assert n != 'D' I tried adding 'n' as a

Customizing error message for specific exceptions in pytest

℡╲_俬逩灬. 提交于 2020-01-01 07:38:09
问题 I'm trying to write a pytest plugin to customize the appearance of specific exceptions - more specifically, mocking exceptions (method expected to be called was not called etc.), because there's a lot of useless noise in the traceback of those exceptions. This is what I've got so far, which works, but is extremely hacky: import pytest import flexmock @pytest.hookimpl() def pytest_exception_interact(node, call, report): exc_type = call.excinfo.type if exc_type == flexmock.MethodCallError:

In pytest, how can I figure out if a test failed? (from “request”)

ⅰ亾dé卋堺 提交于 2020-01-01 04:19:11
问题 I'm using Selenium with PYTEST to test a site. I would like to take a screenshot of the page whenever a test fails (and only when it fails). Is there a way that I can do this? The docs are quiet when it comes to this (or I can't find it). I would assume that it would be something like request.function.failed and it would return a boolean or something. This is what I wanted to do: @pytest.fixture() def something(request): if request.function.failed: print "I failed" This would be added to a

How can I use a parametrized dependent fixture twice in pytest?

好久不见. 提交于 2019-12-31 22:35:13
问题 I'm trying to use a parametrized fixture multiple times in a single test, with the intent of getting a cartesian product of all of its values. https://stackoverflow.com/a/39444098/102441 shows how to do this for a simple fixture: import pytest @pytest.fixture(params=[0, 1, 2]) def first(request): return request.param second = first # runs 3x3 = 9 times def test_double_fixture(first, second): assert False, '{} {}'.format(first, second) However, this approach falls apart if the parametrization

Can I run line_profiler over a pytest test?

筅森魡賤 提交于 2019-12-31 20:06:52
问题 I have identified some long running pytest tests with py.test --durations=10 I would like to instrument one of those tests now with something like line_profiler or cprofile. I really want to get the profile data from the test itself as the pytest setup or tear down could well be part of what is slow. However given how line_profiler or cprofile is typically involved it isn't clear to me how to make them work with pytest. 回答1: Run pytest like this: python -m cProfile -o profile $(which py.test)