Roland van Laar
Roland van Laar

Reputation: 172

Unhelpful output from pytest

TLDR: How can I get better output from pytest?

I'm using Django with regular python3 unittests. I've just switched to pytest-django for running tests.

pytest throws an error for almost all my tests (149 in total). Pages and pages with this error.

self = <RegexURLResolver 'project.urls' (None:None) ^/>

    @property
    def reverse_dict(self):
        language_code = get_language()
        if language_code not in self._reverse_dict:
            self._populate()
>       return self._reverse_dict[language_code]
E       KeyError: 'en-us'

Which wasn't the problem. It led me down to a wrong path.

I had a syntax error in one of my views.py files. ./manage.py test resulted in:

snip

File "/home/roland/project/views.py", line 20 code = zip(list1, list2])

SyntaxError: invalid syntax

Notice the last: ] which was the problem.

So: How can I get more useful output on problems when using pytest?

Btw: After finding this and scrolling back into the pytest output there was mention of the syntax error. It was just buried in the output.

Upvotes: 1

Views: 284

Answers (1)

Scott Skiles
Scott Skiles

Reputation: 3847

You can use the --maxfail=1 option so it will stop immediately on first failure.

Also, make sure your pytest.ini is setup properly so that pytest knows it should be using django-pyest.

[pytest]
DJANGO_SETTINGS_MODULE='myapp.settings'

For my workflow, I usually do the following:

  • run pytest --maxfail=1 myfile.py &> pytest-output.txt
  • tail, grep, or search he text file for errors.
  • Fix and iterate

There are a lot of other configuration options that will help you to get more meaningful input from pytest.

Upvotes: 1

Related Questions