Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Django apps tests in PyCharm

I am having a hard time trying to configure tests running in PyCharm.

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

Django apps in options field

It works okay mostly (tests run, succeed), though it doesn't let you re-run individual tests and re-run failed ones - it always runs all tests for specified applications (common and authorization).

The manual says I should put django application names in "Target" field like this:

Django apps in Target field

But whenever I do it, my tests fail to run with weird errors: sometimes it cannot import some modules, though they are definitely accessible, sometimes there are a lot of NoReverseFound exceptions, though none of them are actually present in the code.

I suppose I am configuring something wrong, but I cannot understand what. I am running the latest version of PyCharm and one of the 1.5.x versions of Django (some legacy code from back in the day I had to maintain)

UPD: If I put authorization.UserApiTestCase.test_login in target - it works great, authorization.UserApiTestCase works too, but putting just appname (authorization) won't work and will produce NoReversrMatch or import errors... leaving Target empty will work, too, though it will run even iinternal Django tests and that's not what I need - I just want to run all my apps' tests (or tests from specific apps).

like image 709
DataGreed Avatar asked Jul 27 '15 10:07

DataGreed


1 Answers

If your project structure in PyCharm looks similar to this:

myproject
├── README.md
├── myproject
│   ├── app1
│   │   └── test.py
│   ├── app2
│   │   └── test.py
│   ├── app3
│   │   └── test.py
│   └── settings.py
└── requirements.txt

Then you may need to add the second level myproject folder as a "Sources Root"

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

To add a "Sources Root" go to Preferences -> Project:MyProject -> Project Structure

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

After that you should be able to run tests with specified targets.

like image 55
quaspas Avatar answered Oct 20 '22 02:10

quaspas