How to run unittest in pycharm community v2018.3.6 - python

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.

Related

How can I use debugger with unit test in Python with Pycharm?

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

Debugging htcondor issue running python script

I am submitting a python script to condor. When condor runs it it gets
an import error. Condor runs it as
/var/lib/condor/execute/dir_170475/condor_exec.exe. If I manually copy
the python script to the execute machine and put it in the same place
and run it, it does not get an import error. I am wondering how to
debug this.
How can I see the command line condor uses to run it? Can the file
copied to /var/lib/condor/execute/dir_170475/condor_exec.exe be
retained after the failure so I can see it? Any other suggestions on
how to debug this?
You can simply run an interactive job (basically just a job with sleep or cat as command) and do ssh_to_job to run it.
Generally you need to set-up your python environment on the compute node, it is best to have a venv and activate it inside your start script.

Pycharm/IntelliJ shows 0% coverage for pytest even though coverage was generated

I have a Python project and a tests task, set up to run pytest from the project's working directory.
Doing Run 'tests' with coverage from the Run menu successfully runs the tests, and the console results shows that coverage was measured - e.g. 53% cover for mws.py.
The automatically applied coverage (as on the right) is 0% for all files, I'm not sure why. I'm using IntelliJ 2017.2.2 EAP.
NB: there is a related five year old question here, but the top rated solution there doesn't apply. There is no error message in the results console in this case.
I had a similar issue, but the accepted solution here didn't solve it.
I had pytest automatically run coverage in its configuration file.
In PyCharm, I added a Run Configuration to run all my tests with pytest.
It seemed to work, and I saw all tests running and got their results to display in PyCharm's run window.
But soon I noticed there were two problems:
When I selected "Run with coverage" I got an error like "coverage results not found", and all files showed 0% coverage.
Breakpoints in tests were not hit when running test in Debug mode.
Both problems disappeared when I added --no-cov to the "Additional Arguments" passed to to pytest (this option is in the Run Configuration).
So It seems the fix was to tell pytest to not run coverage when running it from PyCharm. Both "Run with coverage" option and the Breakpoints in tests now work.
I think the problem lies in you use pytest-cov, so Pycharm cannot parse the result which is shown in text like 53% generated by pytest-cov;
So Changes option in pytest.ini to addopts = -s -v when you want to use Pycharm built in coverage tools.
In this command -v stands for verbosity and -s for disabling all output.
Take a look at my answer for other question about same issue: https://stackoverflow.com/a/45729723/1229510
Basically if you use symlinks - coverage display won't work.

Can PyCharm drop into debug when py.test tests fail

When running tests with py.test there is a --pdb option to enter pdb on failure.
Is there a similar way to enter the debugger when running the same test from within PyCharm?
There is a py.test plugin, pytest-pycharm, that will halt the PyCharm debugger when a test emits an uncaught exception.
Follow the steps below to setup the correct run configuration:
Run > Edit Configurations...
Click the '+' button to add a new configuration.
Name the configuration and specify the configuration parameters below:
Script: The path to your py.test executable (find by typing which py.test)
Script Parameters: This parameter is your test case followed by the --pdb option (ie /Users/Johan/projects/misc/testing.py --pdb)
After setting up the configuration, you can run the test case from within PyCharm. If a test case fails, your PyCharm run window will show the pdb prompt.

Getting Unit Tests to work with Komodo IDE for Python

I've tried to run the following code on Komodo IDE (for python):
import unittest
class MathLibraryTests(unittest.TestCase):
def test1Plus1Equals2(self):
self.assertEqual(1+1, 2)
Then, I created a new test plan, pointing to this project(file) directory and tried to run it the test plan. It seems to run but it doesn't seem to find any tests.
If I try to run the following code with the "regular" run command (F7)
class MathLibraryTests(unittest.TestCase):
def testPlus1Equals2(self):
self.assertEqual(1+1, 2)
if __name__ == "__main__":
unittest.main()
it works. I get the following output:
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
What might I be doing wrong?
For the test file to be picked up the filename must start with test_. I tried using just test.py which failed, however test_.py works like a dream.
All you need to do is rename your file. This is not made very clear in the documentation - I worked it out via a bug report on Komodo's web site.
It would be nice if Komodo gave at least a clue to the problem!

Categories

Resources