Disable coverage while running nosetests - python

I'm new to Python. I am new to PyCharm.
I am trying to debug through my unit tests. They are done with nosetest.
Currently, when I run my tests with the vagrant debugger, it gives me the following complaint:
PYDEV DEBUGGER WARNING:
sys.settrace() should not be used when the debugger is being used.
This may cause the debugger to stop working correctly.
If this is needed, please check:
http://pydev.blogspot.com/2007/06/why-cant-pydev-debugger-work-with.html
to see how to restore the debug tracing back correctly.
Call Location:
File "C:\Python27\lib\site-packages\coverage\collector.py", line 248, in
_installation_trace
sys.settrace(None)
Just going off the call location, I am assuming it does some manner of coverage while running and that the PyCharm debugger does not get along with the coverage library that nosetest is running.
If I run 'nosetests --plugins', I get the following output:
Plugin capture
Plugin failuredetail
Plugin xunit
Plugin deprecated
Plugin skip
Plugin multiprocess
Plugin logcapture
Plugin xcoverage
Plugin coverage
Plugin attributeselector
Plugin doctest
Plugin profile
Plugin id
Plugin allmodules
Plugin collect-only
Plugin isolation
Plugin pdb
Plugin timer
Is there a way to turn off coverage for my test run while I debug?

pytest-coverage does not work with Pycharm - use the --no-cov parameter in the PyCharm test runners. If you want the coverage in then use the PyCharm's coverage runner (with the --no-cov parameter) to use the coverage that comes with PyCharm.

Related

Cannot debug pytest tests in Visual Studio 2022 -- symbols not loaded for my python code

I'm trying to debug pytest tests in Visual Studio 2022. I have the tests showing up in test explorer and when I run them they execute, however when I set a breakpoint and try to debug the test the breakpoints show the warning symbol and when I hover over it, it informs me that the breakpoint will not be hit because symbols are not loaded. The test does actually run, I just can't figure out how to get it to load whatever it needs to in order for me to debug.
It's python so there's no module to inspect in the modules window to tell it to load symbols for and nothing is compiled so I don't know where symbols would even be. Is there something that needs to be configured in the pyproject (besides setting the test framework to pytest)? Debugging in my other python projects just worked out of the box. Is there a setting for loading symbols for your own python code? If so, does anyone know where or why it would be disabled for pytest tests while testing?
It turns out this was a bug in VS. Simply updating to the latest version resulted in everything magically working as expected.

Using pytest, is it possible for a unit test to know that it is being run with code coverage monitoring on?

I am currently developing some tests using python py.test / unittest that, via subprocess, invoke another python application (so that I can exercise the command line options, and confirm that the tool is installed correctly).
I would like to be able to run the tests in such a way that I can get a view of the code coverage metrics (using coverage.py) for the target application using pytest_cov. By default this does not work as the code coverage instrumentation does not apply to code invoked with subprocess.
Code Coverage of the code does work if I update the tests to directly invoke the entry class for the target application (rather than running via the command line).
Ideally I want to have a single set of code which can be run in two ways:
If code coverage monitoring is not enabled then use command line
Otherwise execute the main class of the target application.
Which leads to my question(s):
Is it possible for a python unit test to determine if it is being run with code coverage enabled?
Otherwise: is there any easy way to pass a command line flag from the pytest invocation that can be used to set the mode within the code.
Coverage.py has a facility to automatically measure coverage in sub-processes that are spawned: http://coverage.readthedocs.io/en/latest/subprocess.html
Coverage sets three environment flags when running tests: COV_CORE_SOURCE, COV_CORE_CONFIG and COV_CORE_DATAFILE.
So you can use a simple if-statement to verify whether the current test is being run with coverage enabled:
import os
if "COV_CORE_SOURCE" in os.environ:
# do what yo need to do when Coverage is enabled

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.

Where does console output go when Eclipse PyUnit Test Runner configured to use Nose

I'm using Eclipse / PyDev and PyUnit on OSX for development. It was recommended to me that I use Nose to execute our suite of tests.
When I configure Nose as the test runner, however, output from the interactive console (either standalone or during debugging) disappears. I can type commands but do not see any output.
Is this normal, or am I missing some configuration?
I eventually found in the Preferences > PyDev > PyUnit menu that adding -s to the Parameters for test running stopped this. The parameter prevents the capture of stdout that nose does by default.
The alternate --nocapture parameter should work too.

How to get Eclipse + PyDev + App Engine + Unit testing to work?

I want to run my unit tests for a Python Google App Engine project using
Run As => Python unit-test
But when I try that all my Model tests bail with the error message:
BadArgumentError: app must not be empty.
Anyone got this to work?
NB: The tests runs fine using Nose --with-gae. But I want the PyDev integration with hyperlinking of resources and such.
Pasting the answer I got from the Fabioz (the PyDev creator) himself over at the PyDev forums on SF: https://sourceforge.net/projects/pydev/forums/forum/293649/topic/3618848
There's no such option right now... please enter a feature request for that. Note that you can run nose itself from inside of pydev (with the --with-gae option) -- which at least would give you hyperlinking inside of pydev -- to do that just create a custom run where nose is the main script.
Indeed, that's what I did and it works as advertised. I also entered that feature request. You can help by pushing your support behind the request: https://sourceforge.net/tracker/?func=detail&aid=2974043&group_id=85796&atid=577332

Categories

Resources