“Empty test suite” by running Django tests with Py

2019-07-19 06:20发布

I'm having an hard time trying to run my Django tests using PyCharm. I did it easily in the past using a "python running configuration" in which I used manage.py to run them, I was on OSX and using python 2.7, Django <= 1.7, Pycharm <= 4. Now I'm on Linux Mint, I use Python 3.4.3, Django 1.8.2 and Pycharm 4.5. I always used virtualenv to manage my projects. Now by running "manage.py test" from Pycharm no tests are found (it prints "Empty test suite"). But tests exist, and if I run the command from the shell they get executed. So I really don't understand what's going on. I'm actually able to launch tests using "an hack" I created, but the problem is that Pycharm is not able to attach to the process in order to allow breakpoints in tests (which is one of the features I use the most!). My "hack" is the following:

I created a python script with:

import subprocess


def main():
    subprocess.call('./venv_activator.sh', shell=True)


if __name__ == '__main__':
    main()

then a bash script (venv_activator.sh):

#!/usr/bin/env bash
source .ENV/bin/activate
python manage.py test myapp -p "*_tests.py" --noinput -v 2 --settings=settings.test

In practice the python script is used to create a "python running configuration" for Pycharm, then it activate the virtualenv and invokes the django test command.

ps: yes, I turned on "Django support" in PyCharm (and I configured the python interpreter under the virtualenv)

3条回答
ゆ 、 Hurt°
2楼-- · 2019-07-19 06:41

For my case it was because the names of my test methods didn't start with test. Renamed them, and PyCharm started to run them correctly.

查看更多
闹够了就滚
3楼-- · 2019-07-19 06:43

Using pycharm, you can directly run your tests by creating a test configuration. Create a configuration using Run>Edit Configuration. Add a config from Django tests , set the virtual env path , working directory(path to manage.py), Target module (like complete_project or a complete_project.app_name). Run the configuration , it will run the tests from the tests.py from corresponding module.

查看更多
虎瘦雄心在
4楼-- · 2019-07-19 06:48

The problem is that PyCharm doesn't apply the given search pattern for test files (-p "*_tests.py"). It's a bug that has already been reported: https://youtrack.jetbrains.com/issue/PY-15869

The solution has been to simply rename my files :P

查看更多
登录 后发表回答