Python 在Visual studio 中做单元测试进行TDD开发

本文详细介绍了如何在Visual Studio中使用Python单元测试,包括如何导入、定义和运行测试,以及如何通过测试发现、执行和调试代码。文章还提供了在Visual Studio中设置测试环境、发现和执行测试模块的步骤,并讨论了测试运行和调试过程中的关键操作和注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Unit Tests

Steve Dower edited this page on 14 Jul ·  3 revisions

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"
  • (Optional) Add a call to unittest.main()
    • This will 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".

New 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__': unittest.main()

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.

test_A

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.

Tests Group Menu Grouped by Class Grouped by Filter Grouped by Outcome

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.

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

test_A passed

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_A failed

test_A failed with reason

Debugging Tests

Tests can be debugged 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.

Debug Test

Debugging a test

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值