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 !
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.
Mainly, you will need to fill "Script path" (path to manage.py) and "Parameters".
Then, run debug with that configuration and breakpoints in Django tests will work.
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
Related
I am trying to run unit test in pycharm but not seeing an option in the context menu..
Please find as show below
Please help
Also, when I run from the following command from the console, 'python -m unittest login.py' unit test are running but again if I run command 'python login.py' actual test execution is not running.
How to run unit test and test execution from console or from pycharm IDE ..??
Looks like you have an existed run configuration (login) for this script so PyCharm suggests it instead of running tests. Either remove it from Run | Edit configurations and try to right-click -> Run unittest or use Run | Run... and select unittest there. Has it helped?
Thank you Pavel for respond, I have solved by renaming file name starts with 'test', it worked now, not sure if this is really the way to fix but it resolve my problem.
I have 60 functional tests in one file. I wrote them in Notepad++ and used py.test as the test framework. Today I decided to swap Notepad++ with PyCharm. I opened my file of functional tests in PyCharm and ran the tests from PyCharm, as you can see in the picture:
Now, after confirming that I could run all of the tests, I tried to run an individual test, for example test_login_with_extantUser_using_email. Logically, I right-clicked on the test, expecting a "run test" button or something similar to appear. But no such thing appeared. In fact, it appears that there is no way to run an individual test by simply right-clicking on it.
So my question is, how can I run an individual test? Must I set up a configuration for each one in the Edit Configurations menu? That would take a very long time, considering that I have 60 tests.
I have found the problem. It's a bug in Pycharm that needs to be fixed. I will explain the bug.
Right-clicking an individual test will not display the option to run the test if the test is not a member of a class that inherits from unittest.TestCase. This is true even if you are not using unittest, as in my case, in which I am using py.test.
When I made my py.test test classes inherit from unittest.TestCase, I got the option to run tests individually when I right-clicked on a test.
I have reported the bug to Pycharm. Time will tell if they fix it.
https://youtrack.jetbrains.com/issue/PY-26754
Base on pt.test doc you can specify particular test_case for running with -k (which means keywords obviously). In your case it should be like pytest -k "test_login_with_extantUser_using_email" if you running in CLI.
If you want to run particular test with Pycharm, you have to fill 'Keywords' field in Run/Debug Configurations.
Best Regards.
I'm upgrading a Django app from version 1.10.7 to 1.11.2.
After the upgrade, runserver command works well, but when I try to run the unit test suite the following error raised:
CommandError: Option `--functions=...` couldn't be empty
To run the unit test suite, I'm using the following command:
python src\main\manage.py test --noinput
As you can see, I'm not using any --function parameter and this is the same command that I used before the upgrade.
I reach the same situation if I run the test suite specifying the file to run.
I have also tried to run the help function for the manage command and no --functions parameter is showed.
I'm a little bit confused on this issue, and I don't find any information about this error in both, documentation and release notes.
Anyone knows how this error is raised and how to fix it?
I'm running Django 1.11.2 and Python 2.7
Thanks in advance
It looks like you've got a custom management command called check that conflicts with one of Django's built-in commands. For whatever reason, manage.py test is calling your custom command instead of the built-in one.
Renaming your custom command to something that doesn't conflict should solve the problem.
I'm new to unit testing and "im trying to run a pytest on Eclipse. I have searched for hours now and I cant seem to find out what the problem is. Im playing around with the simple examples from the https://pytest.org website. My problem is that pytest just does not run on Eclipse. I can use the command prompt to do the tests, but I would much rather have the results on the console window.
Things I have tried but didnt work;
setting PyUnit test runner to Py.test runner (instead of the default Pydev test runner)
In this case I get the following error message
usage: runfiles.py [options] [file_or_dir] [file_or_dir] [...]
runfiles.py: error: unrecognized arguments: --verbosity inifile:
None rootdir: C:\peepee\pytest\testing
I have set the verbosity to 9 (read somewhere that its the maximum). Didnt make any difference.
Simple code I'm trying to test from the http://pytest.org website
def func(x):
return x + 1
def test_answer():
assert func(3) == 5
Works through the cmd but not on Eclipse.
Please help, as I'm losing time on trying to figure this out. Thanks in advance
It didn't work for you because you left "--verbosity 0" in the parameters text field. I don't know why it's not automatically erased by Eclipse, but when you change the runner you MUST also change the parameters to reflect your preferred test runner (pytest in this case).
Globally for all new configurations:
Window -> Preferences -> PyDev -> PyUnit
Change the test runner to "py.test runner" and clear the parameters (or add the ones you prefer. Make sure they are valid flags for pytest.)
Or, if you prefer, manually for each new run configuration
Create a Python unittest run configuration
Select the class you want to run and the project (if they are not already there)
In the Arguments tab override the pyUnit preferences with a py.test runner (clean up the parameters and add whatever you want to add to pytest flags)
1) open run configurations from run menu
2) right click on python unittest and select new to configure a new configuration
3) select the project and the in the main module, select the module which has your test cases.
4) under arguments tab, check 'override pyunit preferences for this launch'.
5) select Py.test runner from the drop down.
6) type --tb=short and --capture=no (values can be changed depending on user preference)
7) click apply and then click run.
NOTE: If you are using django-configurations for your settings then you MUST set the DJANGO_CONFIGURATION environment variable in the environment variables section of the debugger configuration to whatever you use for your testing runs from the command line.
Found an alternative. Just use the PyCharm IDE which makes pytest very easy to run. Make sure to do the following configuration before running any test.
Click the "Run" tab. Select "Edit configurations"
Add configuration with the "+" symbol
Select "Python test" under that "py.test"
Make sure to fill out the "Target" path and the "working directory"
Happy days. Now you have pytest running with the results displayed on the console window
I'm creating a python test suite (using py.test). I'm coding the tests in Idea and I don't know how to debug a single test.
This is my setting of the debugger. It runs the whole testsuite. So I have to run all the tests before it gets to the one I'm trying to debug.
In your configuration, set:
Target to the relative path of one of your test files, i.e. testsuite/psa/test_psa_integration.py
Keywords to a keyword that identifies the test you are trying to run specifically. If tests are part of a class, Keywords should be something like: TestPsaIntegration and test_psa_integration_example
I don't use IntelliJ, but in PyCharm, you can easily debug tests without going through this tedious process of adding a Run/Debug configuration each time.
To do this with PyCharm, go to:
Preferences (or Settings) > Tools > Python Integrated Tools and set Default test runner to py.test.
Then, back in your file (i.e. test_psa_integration.py), you could just right-click anywhere within the code of a test, and select either Run 'py.test in ...' or Debug 'py.test in...' which will automatically create a new Run/Debug configuration as explained previously.
An alternative is adding --no-cov --capture=no into Additional Arguments. To make this automatic for other test file, add those into the Template part.