Unit Tests

Note: Unit tests are only supported on Visual Studio 2012 Professional and higher. The Visual Studio Shells and any version of Visual Studio 2010 do not detect or run unit tests.

Unit tests are short sections of code that test small pieces of functionality belonging to a larger program. By demonstrating that each piece of a program is correct, it is easier to infer that the entire program is correct.

Python uses unit tests extensively to validate scenarios while designing a program. Python Tools for Visual Studio includes support for discovering, executing and debugging unit tests. This allows you to author your tests and run them without having to switch to a command prompt.

Discovering Tests

PTVS will discover tests using the standard unittest package. To ensure your test can be found and run, follow these rules:

  • Import unittest
  • Derive a class from unittest.TestCase
  • Define a method named "test<name of your test>"
  • (Optional) Add a call to unittest.main()
    • This will allow you to run your script directly to execute the tests

Blank tests that follow these rules can be added by selecting Project, Add New Item (Ctrl+Shift+A) and choosing "Python Unit Test".


This will add a new file containing a basic unit test.

import unittest

class Test_test1(unittest.TestCase):
    def test_A(self):
        self.fail("Not implemented")

if __name__ == '__main__':
Test Explorer window showing test_A

Hit Save All (Ctrl+Shift+S) to save the project file, and your test will be discovered and displayed in the Test Explorer. (If you do not see the Test Explorer window, click the Test menu, then Windows and Test Explorer.)

Important: If you do not see your test in the Test Explorer window, check our beta caveats for known issues and workarounds.

As you add more tests to your project, you may prefer to group or filter the tests that are displayed. The "Group By" menu on the toolbar will allow you to collect your tests into different groups, and the search toolbox will filter by matching names. Double-clicking a test will open the source file containing the test implementation.

The Group By menu in Test Explorer Tests grouped by Outcome
Tests grouped by Class Tests grouped by Filter

Running Tests

Tests can be run by clicking "Run All" in the Test Explorer window, or by selecting one or more tests or groups, right-clicking and selecting "Run Selected Tests". Tests will be run in the background and the display will be updated to show the results.

Test Explorer showing test_A has passed

Tests that pass are shown with a green tick. The amount of time taken to run the test is also displayed.

Test Explorer showing test_A has failed

Tests that fail are shown with a red cross. The "Output" link can be clicked to display the text that was printed to the console during the test, including the standard unittest output.

Test Output window showing the reason test_A failed

Debugging Tests

Tests can be debugging by right-clicking a test and selecting "Debug Selected Tests". (Note that "Analyze Code Coverage for Selected Tests" and "Profile Test" are not supported.) Ensure you have set a breakpoint in your test. When the breakpoint is hit, the normal debugging experience is available until the test completes.

DebugTest.png DebuggingTest.png

Known Issues

  • When starting debugging, VS will appear to start and stop debugging, before starting again. This is expected.
  • When debugging multiple tests, each one is run independently, which will interrupt the debugging session.
  • VS will intermittently fail to start a test when debugging. Normally, attempting to debug the test again will succeed.
  • When debugging, it is possible to step out of a test into the unittest implementation. Normally, the next step will run to the end of the program and stop debugging.

Last edited Oct 10, 2013 at 7:00 PM by Zooba, version 13


GreenKNight Sep 26, 2014 at 1:57 PM 
Is there any way I can run django unit tests using Test Explorer integration? When I run them at the moment I get "Models aren't loaded yet" from django 1.7. I presume I need to pass command line parameters to the python test runner to give it the correct settings file, but don't know how.

pminaev Apr 29, 2014 at 7:35 PM 
brucegk, can you clarify what the issue is? The bitness of the test runner itself should not matter, and Python code should run using whatever Python interpreter is active for your test project. Or are you saying that, if a 64-bit interpreter is active, you get an exception when trying to run the tests?

brucegk Apr 29, 2014 at 7:00 PM 
the VS test runner only has the option between x86 and x64, and python test projects only seem to run with the x86 setting (otherwise throwing an exception). This is a problem for x64 mixed code solutions. There should be a way to make this compatible but there are no platform settings for the python projects.

Ptools Aug 13, 2013 at 9:19 PM 
meertn - the basic version (Integrated PTVS + VS Shell) doesn't support Profiling or Unit Tests :(. Sorry about that - unfortunately not in our hands.

meertn Jul 3, 2013 at 6:03 AM 
"If you do not see the Test Explorer window, click the Test menu, then Windows and Test Explorer." I don't see the Test Explorer window, nor even the Test menu :( I must be doing something wrong but I can't figure it out. I downloaded and installed the 2013 “PTVS + VS Shell” version, and opened a new empty project. I added a unit test file to this, but I don't see any of the special test features. I have a regular Python3.3 interpreter installed.