Getting Unit Tests to work with Komodo IDE for Python - 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!

Related

How to run unittest in pycharm community v2018.3.6

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.

How to run a particular test in PyCharm

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.

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.

Running pytest from inside a module, seems to cache tests

I recently started playing with pytest and I use pytest.main() to run the tests. However it seems that pytest caches the test. Any changes made to my module or to the tests gets ignored. I am unable to run pytest from command line so pytest.main() is my only option, this is due to writing python on my ipad.
I have googled this extensively and was able to find one similar issue with advice to run pytest from command line. Any help would be greatly appreciated.
Thanks,
Pytest doesn't cache anything. A module (file) is read once and only once per instance of a Python interpreter.
There is a reload built-in, but it almost never does what you hope it will do.
So if you are running
import pytest
...
while True:
import my_nifty_app
my_nifty_app.be_nifty()
pytest.main()
my_nifty_app.py will be read once and only once even if it changes on disk. What you really need is something like
exit_code = pytest.main()
sys.exit(exit_code)
which will end that instance of the interpreter which is the only way to ensure your source files get re-read.

Configuring Python in Aptana to push output to console window

I have very recently been learning python. I have been using pyscripter as is demonstrated over on kahnacademy, but after finding some bugs in pyscripter decided to upgrade to a much more robust coding environ, hence aptana studio.
Right now I have python installed properly, it will run scripts, but I cannot seem to figure out how to get it to output print commands to the console window at the bottom.
To test this my script is simply:
print "hello"
The console gives me:
Finding files... done.
Importing test modules ... done.
---------------------------------
Ran 0 tests in 0.000s
OK
I know I am missing something really simple here but I haven't the foggiest what it could be.
I would like to see the python console output only - what could I be missing?
You're probably using the 'run unit-tests' launch instead of the regular launch. For a regular launch, just press F9 on the file you're editing and it should work. Also, if you still haven't, read the PyDev getting started guide: http://pydev.org/manual_101_root.html (or at least http://pydev.org/manual_101_run.html, which is the part related to your problem, and it'll also explain how to config Ctrl+F11 to relaunch your last launch, etc).
Aptana uses PyDev.
To run modules (.py) files in debug mode you need to tell the file what function you want to execute. Try this template when testing:
def main():
print "Hello world"
if __name__ == "__main__":
main()

Categories

Resources