Reputation: 5095
I have a python file called AllTests.py which looks like the following:
import unittest
from SomeWonderfulTest import SomeWonderfulTestCase
def runAllTestsNow():
theTestSuite = unittest.TestSuite()
theTestSuite.addTest(unittest.makeSuite(SomeWonderfulTestCase)
return theTestSuite
if __name__ == "__main__":
unittest.main(defaultTest='runAllTestsNow')
I can run this at the command line as follows:
python -m unittest AllTests
However, I would like to use the Spyder IDE to debug some issues. I know to specify command line options in Spyder, I can go to Run->General Setting->Command line options. However, when I try to put "-m unittest" into these options, I get an error saying "option -m not recognized." It appears it's trying to send the argument to the AllTests.py file instead of to python.
How do I setup Spyder to debug these unittests?
Upvotes: 5
Views: 14829
Reputation: 3259
This feature is as of today not available in Spyder and its unit-test plugin. There is a ticket in the project's bugtracker requesting it since about one year, with no visible progress: https://github.com/spyder-ide/spyder-unittest/issues/126
As a workaround, you can use the following script:
# Entry point for pytest
#
# This allows debugging of pytest unit tests in Spyder (or command line).
# In Spyder, set your breakpoints as required, then run this script
# (no need to debug, run is enough).
# The debugger will break automatically on the pytest.main().
# Continue the execution to jump to your own breakpoints.
import pdb
pdb.set_trace()
pytest.main()
Save it, for instance as "main_pytest.py", and run it in Spyder. No need to debug. The debugger will stop automatically on the pytest.main(). Continue the execution to jump to your own breakpoints.
It uses the standard Python debugger to force the debugging. See here for the documentation: https://docs.python.org/3/library/pdb.html
The advantage of this approach is that it doesn't require to modify the actual code.
As a bonus, you can use all the possible options from the documentation (https://docs.pytest.org/en/latest/usage.html) as parameters to pytest.main().
Upvotes: 1
Reputation: 1
To run unit test and code coverage in spyder IDE,
!python -m unittest programname.py
!coverage run -m unittest programname.py
!coverage report -m
!coverage html
To view the coverage html:
Upvotes: 0
Reputation: 2345
Apparently either you can't or it's broken, as there is an open issue in spyder-ide repo about this, as it stands today 9th Jan 2020.
I am using spyder-unittest and as a work around, I would call the test function directly from the test file using debug. so e.g. in case of spyder-unittest if test_file.py contains below test function
def test_something():
assert(True)
then after this function make a call directly to this test function e.g.
def test_something():
assert(True)
test_something()
and put a breakpoint on it. Now instead of running your testfile as unit test, simply hit Debug, which will make your test file run like a normal file under debug mode, this will let you step into your test function and then actual function with all test data setup.
Note: This approach will work in case of simple self contained test cases, but if you have a complicated test setup then it's likely to not work but still you can get some intuition on how to make this work in your case.
Upvotes: 0
Reputation: 81
This post is a bit old but I've been using the following (posted by https://stackoverflow.com/users/9450152/%e3%82%ae%e3%83%a7%e3%83%bc%e3%83%a0):
In the iPython console :
!python -m unittest AllTests
The starting exclamation point gives you direct access to the shell.
I've found that all you really need is:
!python -m unittests
This will discovery all tests files with pattern of "test*.py" from the current working directory. The current working directory is shown in the toolbar or you can type the following in the ipython console:
%pwd
What happens is that sometimes I will run a test file (by pressing F5 or clicking the Run arrow). This will change the working directory to where that file is located. The command above will then only search for test files starting from that directory (btw, I wasted a lot of time trying to figure this out). You can change the working directory from the toolbar (click the up arrow) or use:
%cd ..
repeatedly in the console until you reach the project directory.
Upvotes: 3
Reputation: 2404
In the iPython console :
!python -m unittest AllTests
The starting exclamation point gives you direct access to the shell.
Upvotes: 1
Reputation: 1735
You can use the module spyder-ide/spyder-unittest.
https://github.com/spyder-ide/spyder-unittest
To install it, use:
sudo pip install spyder-unittest
Then restart your computer. Open Spyder. In the right top corner you will find a new tab: Unit Testing. Click on it. You will see a green arrow with the label Run test. This is the button you will use to run your tests.
When you click on the arrow for the first time a pop-up window will come out. You can access this pop-up window by clicking on the settings gear (top left corner of this panel) -> Configure.
Choose the test framework. I assume you already installed unitest
. You should have it as an option. Other options are py.test
nose
.
Choose a folder in the where your scripts test_<name>.py
will be located. Best practices state you should create a folder called unittests
inside the folder where the scripts being tested are located:
my_python_scripts_folder/
-> unittests/
test_<name1>.py
test_<name2>.py
test_<name3>.py
<name1>.py
<name2>.py
<name3>.py
MIND THE NAME CONVENTION, all the scripts that contain tests should have a name that starts with tests_
. Your scripts test_.py should look like:
import unittest
import sys
sys.path.append("../") # this adds the mother folder
# "my_python_scripts_folder/" to the python path
# It will allow you to import your modules.
# Adjust depending where your tests scripts location
from <name1> import Stuff
class TestClass(unittest.TestCase):
def test_ImportFromFile(self):
<do stuff with your Stuff>
self.assertIsNone(Stuff)
self.assert ... etc.
all your test methods should have a name that starts with test_
.
Check: https://docs.python-guide.org/writing/tests/ for more info.
You can debug your tests scripts using the debug tools in spyde's IDE
Upvotes: 3
Reputation: 416
So I am fairly new to Spyder, as we are using Visual Studio to do our dev, however the principals appear similar.
So, how we have things working is as follows:
IDE is the interface you use to do your development. (Spyder in your case, or VS in mine.)
Anaconda is (my) the Python Environment which reads and executes the python scripts you write. This gives you the interpreter window, etc. The IDE uses the Python Environment to run your scripts.
You can run your tests in a few different ways.
You have created a wrapper class/method called AllTests.py which is a python script.
This AllTests.py script can be ran in the console directly as you have outlined in your question.
You could also run your AllTests.py script as you would any other script, as outlined by carlos.
You can also install an addon into your IDE (Spyder / VS / etc) which interprets all UnitTest classes, and displays them in a visual way, instead of using the command line. A Test Explorer (as it is called in VS), or alternative addon would look through your code, and find all your tests, group them together in various different ways, allow you to execute them by name / group / etc, and give you further information on the tests and their outputs. (Execution time, pass/fail outputs, etc,)
One I found for Spyder is here: https://pypi.python.org/pypi/spyder-unittest
Not sure if it is any good, as I've not used it. Someone else here may be able to comment on it's ability/performance.
Below is a screenshot of the Visual Studio Test Explorer:
Depending on your requirements and development practices, this testing addon may be a better option for you.
I hope this is of some help.
Upvotes: -1