3

I have the following problem :

I am doing some unit tests but the problem is that I cannot use the debugger I tried to click on "Debug namefile" using a breakpoint but it does not work. Alternatively, I tried to use tyhe following decorator @override_settings(DEBUG=True) but once again I had no result using this way.

I precise that it is only with unit tests I have this kind of problems. The other part of the code works well.

Could you help me please ?

PS: to do the unit test I imported TestCase from django.test.

Thank you very much !

6
  • What do you mean by "it does not work"? What PyCharm version are you using? Commented Aug 30, 2019 at 12:40
  • I mean It is like I click on Run without the mode debug active Commented Aug 30, 2019 at 12:45
  • That doesn't clear things up. Please add more details (maybe even screenshots), as described in [SO]: How to create a Minimal, Reproducible Example (reprex (mcve)). Commented Aug 30, 2019 at 12:52
  • So you click on Debug Unittests for test_... command in the sidebar next to your test? And in the bottom console, the name of the tab that opens is "Debug"? Commented Aug 30, 2019 at 13:57
  • And you're sure the "Mute breakpoints" option isn't activated? (little red circle with a slash) Commented Aug 30, 2019 at 13:59

3 Answers 3

3

I used to have same problem with PyCharm+Django when running python manage.py test from command line. I solved it by creating new configuration for test.

enter image description here

Mainly, you will need to fill "Script path" (path to manage.py) and "Parameters". enter image description here

Then, run debug with that configuration and breakpoints in Django tests will work.

Sign up to request clarification or add additional context in comments.

Comments

1

Given you already have a command-line way of running your tests, you can easily take that to a Run Configuration in Pycharm (this works for Pycharm Community edition) by telling it to run the manage.py file and then adding parameters needed to run tests.

For example, let's say you have a Django root project folder containing an app folder and then a "tests" subfolder in it ("tests" could even have more subfolders for each test category). So the path would be something like: root_project/app/tests. I assume this tests folder has an __init__.py file and also if there are subfolders within "tests" they would also have an __init__.py each. So a viable command to run these tests could be python manage.py test app.tests.<module>.<file>

Given this command, you can create a Run Configuration in Pycharm by going to Run > Edit configurations > (click on "+") and select Python (just plain "Python", not "Python tests"). Give it any name you want (e.g.: "run unit tests") and then add:

  • "Script" field: the path to your manage.py file (e.g.: project_root/manage.py)
  • "Parameters": whatever you put in your command after python manage.py, so in the example I used above it would be test app.tests.<module>.<file>
  • "Python interpreter": if not automatically selected, choose the same interpreter you're using when you run your tests by command. For example, it could be the interpreter in a virtual environment.

Other fields should be auto-populated, but just in case make sure "Working directory" shows your project root folder and that "Add content roots to PYTHONPATH" and "Add source roots to PYTHONPATH" are selected.

Of course, in this example you would replace "[module]" with whatever subfolder you want to run in your tests folder or just remove it completely if no subfolders exist. And also replace "[file]" with the test file you're trying to run.

run configuration

Just save this configuration and you can use it to run or debug your tests by just selecting the run configuration you created and then clicking on the button you need:

how to use run configuration

Comments

0

PyCharm menu:

Run / Edit configuration / Add Django test / Add Target and options.

For example, here is a mapping from command line to GUI fields,

Run just one test method

./manage.py test --keepdb animals.tests.AnimalTestCase.test_animals_can_speak

Target: animals.tests.AnimalTestCase.test_animals_can_speak

options: --keepdb

.

Ref, https://www.jetbrains.com/help/pycharm/2020.1/run-debug-configuration-django-test.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.