Python does unit testing in Visual Studio for TDD development

Source: Internet
Author: User

Unit Testssteve Dower edited this page on Jul 3 RevisionsPages
    • Home
    • Azure Remote Debugging
    • Azuresdk
    • Bottle and Azure Table Storage on Azure
    • Bottle and MongoDB on Azure
    • Browsing Code Using PTVS
    • Build Instructions for PTVS
    • Cloud Project
    • Code formatting
    • Contributing to PTVS
    • Cross Platform Remote debugging
    • Debugging
    • Development Webframeworks
    • Django
    • Django and MySQL on Azure
    • Show More pages ...
Clone this wiki locallyClone in Desktop

Unit tests is short sections of code, test small pieces of functionality belonging to a larger program. By demonstrating, this piece of a program is correct, it's easier to infer so the entire program is correct.

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

Discovering Tests

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

    • Import UnitTest
    • Derive a class from UnitTest. TestCase
    • Define a method named "Test"
    • (Optional) Add a call to Unittest.main ()
      • This would allow you to run your script directly to execute the tests

To add a module with a test class, select Project, add New Item (ctrl+shift+a) and choose "Python Unit test".

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

Import unittesttest_test1 (unittest. TestCase):    test_a (self.fail ("not implemented")' __main__': Unittest.main () 

Hit Save All (Ctrl+shift+s) to save the project file, and your test would be discovered and displayed in the test Explorer. (If you don't see the Test Explorer window, click the Test menu, then Windows and Test Explorer.)

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

As you add + tests to your project, your may prefer to group or filter the tests that is displayed. The ' Group by ' menu on the toolbar would allow you to collect your tests into different groups, and the search Toolbox would Filter by matching names. Double-clicking a test would open the source file containing the test implementation.

Running Tests

Tests can is run by clicking ' Run All ' in the Test Explorer window, or by selecting one or more Tests or groups, RIGHT-CLI cking and selecting "Run Selected Tests". Tests'll be run in the background and the display would be updated to show the results.

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

Tests that fail is shown with a red cross. The "Output" link can is clicked to display the text that is printed to the console during the test, including the Standa Rd UnitTest output.

Debugging Tests

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

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

Python does unit testing in Visual Studio for TDD development

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.