How can I assert lists equality with pytest

See this: Note: You can simply use the assert statement for asserting test expectations. pytest’s Advanced assertion introspection will intelligently report intermediate values of the assert expression freeing you from the need to learn the many names of JUnit legacy methods. And this: Special comparisons are done for a number of cases: comparing long strings: … Read more

How to run a method before all tests in all classes?

Using session fixture as suggested by hpk42 is great solution for many cases, but fixture will run only after all tests are collected. Here are two more solutions: conftest hooks Write a pytest_configure or pytest_sessionstart hook in your conftest.py file: # content of conftest.py def pytest_configure(config): “”” Allows plugins and conftest files to perform initial … Read more

Importing correctly with pytest

The issue here is that Pytest walks the filesystem to discover files that contain tests, but then needs to generate a module name that will cause import to load that file. (Remember, files are not modules.) Pytest comes up with this test package name by finding the first directory at or above the level of … Read more

Create and import helper functions in tests without creating packages in test directory using py.test

You could define a helper class in conftest.py, then create a fixture that returns that class (or an instance of it, depending on what you need). import pytest class Helpers: @staticmethod def help_me(): return “no” @pytest.fixture def helpers(): return Helpers Then in your tests, you can use the fixture: def test_with_help(helpers): helpers.help_me()

How to parametrize a Pytest fixture

This is actually possible via indirect parametrization. This example does what you want with pytest 3.1.2: import pytest class TimeLine: def __init__(self, instances): self.instances = instances @pytest.fixture def timeline(request): return TimeLine(request.param) @pytest.mark.parametrize( ‘timeline’, ([1, 2, 3], [2, 4, 6], [6, 8, 10]), indirect=True ) def test_timeline(timeline): for instance in timeline.instances: assert instance % 2 == … Read more

Py.test No module named *

Working with Python 3 and getting the same error on a similar project layout, I solved it by adding an __init__ file to my tests module. $ touch tests/__init__.py I’m not great at packaging and importing, but I think that this helps pytest work out where the target App module is located.

How to pass arguments in pytest by command line

In your pytest test, don’t use @pytest.mark.parametrize: def test_print_name(name): print (“Displaying name: %s” % name) In conftest.py: def pytest_addoption(parser): parser.addoption(“–name”, action=”store”, default=”default name”) def pytest_generate_tests(metafunc): # This is called for every test. Only get/set command line arguments # if the argument is specified in the list of test “fixturenames”. option_value = metafunc.config.option.name if ‘name’ in … Read more

unittest Vs pytest

1) First of all, you can declare those fixtures not only in conftest.py, but in every Python module you want. And you can import that module. Also you can use fixtures in the same way as you used setUp method: @pytest.fixture(scope=”class”) def input(request): request.cls.varA = 1 request.cls.varB = 2 request.cls.varC = 3 request.cls.modified_varA = 2 … Read more

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