Running Django application tests in PyCharm

It's hard for me to set up tests running in PyCharm.

I want to run tests for my custom django applications, so my configuration looks something like this:

Django apps in options box

It works fine (tests run, succeed), although it does not allow you to re-run individual tests and re-run failed ones - it always runs all tests for specific applications (general and authorization).

The manual says that I should put the django application names in the "Target" field as follows:

Django applications in the Target field

But whenever I do this, my tests cannot run with strange errors: sometimes it cannot import some modules, although they are definitely available, sometimes there are many NoReverseFound exceptions, although none of them are present in the code.

I suppose that I am setting something up incorrectly, but I cannot understand that. I am running the latest version of PyCharm and one of the versions of Django 1.5.x (some legacy codes from the day I was supposed to support)

UPD : if I put authorization.UserApiTestCase.test_login on the target - it works fine, authorization.UserApiTestCase works too, but setting only appname ( authorization ) will not work and will produce NoReversrMatch or importing errors ... leaving the target empty will also work, even though it will run even iInternal Django tests, and that’s not what I need - I just want to run all the tests of my applications (or the tests from specific applications).

+8
python django unit-testing pycharm automated-tests
source share
2 answers

As it turned out, this is a bug related to older versions of Django. Updating to the latest (1.9.2 at the moment) resolved the problem.

0
source share

If your project structure in PyCharm looks something like this:

 myproject β”œβ”€β”€ README.md β”œβ”€β”€ myproject β”‚  β”œβ”€β”€ app1 β”‚  β”‚  └── test.py β”‚  β”œβ”€β”€ app2 β”‚  β”‚  └── test.py β”‚  β”œβ”€β”€ app3 β”‚  β”‚  └── test.py β”‚  └── settings.py └── requirements.txt 

You may then need to add a second myproject folder as "Sources Root"

The Root Sources folders are indicated by the blue folder icon in the Project window

To add β€œSource Root”, go to β€œSettings” β†’ Project: MyProject β†’ Project Structure

Also check "add source roots to PYTHONPATH" in "Run / Debug Configurations".

After that, you will be able to run tests with the specified goals.

+3
source share

All Articles