ModuleNotFoundError: No module named ‘pytest’

TLDR: I suspect you installed pytest within your system level python site-packages so when you try to run pytest, within your virtualenv, it’s throwing a ModuleNotFoundError since it doesn’t have this dependency installed within your virtualenv. Virtual environments give you a sandboxed environment so you can experiment with potential python libraries for your project, but … Read more

Py.test: parametrize test cases from classes

If you subclass from unittest.TestCase, your test methods cannot have additional arguments. If you simply subclass from object, it will work (though you’ll have to use regular assert statements instead of the TestCase.assertEqual methods. import unittest import pytest class TestCase(object): @pytest.mark.parametrize(“test_input,expected”, [ (“3+5”, 8), (“2+4”, 6), (“6*9”, 42), ]) def test_1(self, a, b): assert eval(a) … Read more

How can I use pytest.raises with multiple exceptions?

Pass the exceptions as a tuple to raises: with pytest.raises( (MachineError, NotImplementedError) ): verb = … In the source for pytest, pytest.raises may: catch expected_exception; or pass expected_exception to a RaisesContext instance, which then uses issubclass to check whether the exception was one you wanted. In Python 3, except statements can take a tuple of … Read more

How to call setup once for all tests and teardown after all are finished

The OP’s requirement was for setup and teardown each to execute only once, not one time per module. This can be accomplished with a combination of a conftest.py file, @pytest.fixture(scope=”session”) and passing the fixture name to each test function. These are described in the Pytest fixtures documentation Here’s an example: conftest.py import pytest @pytest.fixture(scope=”session”) def … Read more

Multiple copies of a pytest fixture

My approach would probably to create a fixture which can generate your objects: @pytest.fixture def thing(request, db): class ThingFactory(object): def get(self): thing = MyModel.objects.create() request.addfinalizer(thing.delete) return thing return ThingFactory() def test_thing(thing): thing1 = thing.get() thing2 = thing.get() Obviously you can make .get() take an argument etc. (PS: Also note there’s no need for the lambda … Read more

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