pytest and Failed: Database access not allowed, use the “django_db” mark, or the “db” or “transactional_db” fixtures to enable it

Solution: import pytest @pytest.mark.django_db class TestExample: def test_one(): … Assume that you’ve created a TestExample class inside your test file and it should be decorated with @pytest.mark.django_db. It should solve your problem.

Does pytest have an assertItemsEqual / assertCountEqual equivalent

pytest does not provide an assertCountEqual, but we can just use unittest’s: import unittest def test_stuff(): case = unittest.TestCase() a = [{‘a’: 1}, {‘b’: 2}] b = [{‘b’: 2}] case.assertCountEqual(a, b) And the output is decent, too $ py.test ============================= test session starts ============================== platform linux — Python 3.6.2, pytest-3.2.1, py-1.4.34, pluggy-0.4.0 rootdir: /home/they4kman/.virtualenvs/tmp-6626234b42fb350/src, inifile: … Read more

coverage.py does not cover script if py.test executes it from another directory

I encountered the same issue when calling “py.test –cov …” from tox. I found a hint on this page: http://blog.ionelmc.ro/2014/05/25/python-packaging/ even though it does not mention this explicitly. Using “–develop” for tox will make sure that coverage data gathering is called from the same directory as coverage analysis. This section in tox.ini made it work … Read more

Chaining tests and passing an object from one test to another

For sharing data between tests, you could use the pytest namespace or cache. Namespace Example with sharing data via namespace. Declare the shared variable via hook in conftest.py: # conftest.py import pytest def pytest_namespace(): return {‘shared’: None} Now access and redefine it in tests: import pytest def test_creation(): pytest.shared = ‘spam’ assert True def test_deletion(): … Read more

pytest ScopeMismatch error: how to use fixtures properly

The db fixture has the function scope for a reason, so the transaction rollbacks on the end of each test ensure the database is left in the same state it has when test starts. Nevertheless, you can have the session/module scoped access to database in fixture by using the django_db_blocker fixture: @pytest.fixture(scope=”module”) def get_all_models(django_db_blocker): with … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)