Python 在Visual studio 中做单元测试进行TDD开发
Unit Tests
Pages 38
- 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 23 more pages…
Clone this wiki locally
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".

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.

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.

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.

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.


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.


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
unittestimplementation. Normally, the next step will run to the end of the program and stop debugging.
Python 在Visual studio 中做单元测试进行TDD开发的更多相关文章
- Visual Studio 中的单元测试 UNIT TEST
原文:Visual Studio 中的单元测试 UNIT TEST 注:本文系作者原创,可随意转载,但请注明出处.如实在不愿注明可留空,强烈反对更改原创出处.TDD(Test-Driven Devel ...
- Visual Studio 中的 Office 和 SharePoint 开发
MSDN Library 开发工具和语言 Visual Studio 中的 Office 和 SharePoint 开发 https://msdn.microsoft.com/zh-cn/libra ...
- Visual Studio中UnitTesting单元测试模板代码生成
在软件研发过程中,单元测试的重要性直接影响软件质量.经验表明一个尽责的单元测试方法将会在软件开发的某个阶段发现很多的Bug,并且修改它们的成本也很低.在软件开发的后期阶段,Bug的发 ...
- visual studio中创建单元测试
1 打开 工具--自定义 2 选择 上下文菜单--编辑器上下文菜单|代码窗口 3 在这里我们可以看到“创建单元测试”这个菜单了,将它移到运行测试菜单下面 4 关闭VS并重启 重启后再对着类名,点击右 ...
- [No0000AE]在 Visual Studio 中调试 XAML 设计时异常
在 Visual Studio 中进行 WPF, UWP, Silverlight 开发时,经常会遇到 XAML 设计器由于遭遇异常而无法正常显示设计器视图的情况.很多时候由于最终生成的项目在运行时并 ...
- 在 Visual Studio 中调试 XAML 设计时异常
在 Visual Studio 中进行 WPF, UWP, Silverlight 开发时,经常会遇到 XAML 设计器由于遭遇异常而无法正常显示设计器视图的情况.很多时候由于最终生成的项目在运行时并 ...
- Python Tool Visual Studio简单使用
由于一直在做.NET的开发,一直用的IDE是VS系列的,所以想用VS也能开发Python,刚好微软提供一个插件PTVS(Python Tool Visual Studio)专门应用于Python开发的 ...
- Visual Studio 2013进行单元测试
使用Visual Studio 2013进行单元测试--初级篇 1.打开VS2013 --> 新建一个项目.这里我们默认创建一个控制台项目.取名为UnitTestDemo 2.在解决方案里面 ...
- 教程:在 Visual Studio 中开始使用 Flask Web 框架
教程:在 Visual Studio 中开始使用 Flask Web 框架 Flask 是一种轻量级 Web 应用程序 Python 框架,为 URL 路由和页面呈现提供基础知识. Flask 被称为 ...
随机推荐
- 移动web app开发框架
文章地址:http://www.cnblogs.com/soulaz/p/5586787.html jQuery Mobile jQuery Mobile框架能够帮助你快速开发出支持多种移动设备的Mo ...
- Codeforces Round #FF (Div. 2)__E. DZY Loves Fibonacci Numbers (CF447) 线段树
http://codeforces.com/contest/447/problem/E 题意: 给定一个数组, m次操作, 1 l r 表示区间修改, 每次 a[i] + Fibonacci[i-l ...
- 【转】在ASP.NET MVC中,使用Bundle来打包压缩js和css
在ASP.NET MVC4中(在WebForm中应该也有),有一个叫做Bundle的东西,它用来将js和css进行压缩(多个文件可以打包成一个文件),并且可以区分调试和非调试,在调试时不进行压缩,以原 ...
- javascript中this、apply、call、bind的用法和区别
参考阮一峰文章链接:http://javascript.ruanyifeng.com/oop/basic.html#toc10
- sublime3 使用技巧
Ctrl+O(Command+O)可以实现头文件和源文件之间的快速切换 Ctrl+Shift+T可以打开之前关闭的tab页,这点同chrome是一样的 Ctrl+R定位函数:Ctrl+G定位到行: 插 ...
- LOADRUNNER8.1卸载
卸载LOADRUNNER8.1后,不能正常又一次安装的问题. Loadrunner 8.1 安装1.下载Loadrunner8.1 (官方英文版) 2.安装Loadrunner8.1 3.破解:htt ...
- C#读取注册表
//1.向注册表中写信息using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"", true)){ if (key ...
- FIR滤波器设计
FIR滤波器的优越性: 相位对应为严格的线性,不存在延迟失真,仅仅有固定的时间延迟: 因为不存在稳定性问题,设计相对简单: 仅仅包括实数算法,不涉及复数算法,不须要递推运算,长度为M,阶数为M-1,计 ...
- 深入浅出 RPC - 浅出篇
近几年的项目中,服务化和微服务化渐渐成为中大型分布式系统架构的主流方式,而 RPC 在其中扮演着关键的作用.在平时的日常开发中我们都在隐式或显式的使用 RPC,一些刚入行的程序员会感觉 RPC 比较神 ...
- Java基础知识强化46:StringBuffer类之判断一个字符串是否对称案例
1. 分析:判断一个字符串是否是一个对称的字符串,我们只需要把字符串的第1个字符和最后1个字符,第2个字符和倒数第2个字符,…… 比较的次数是长度除以2. 方法1:通过取取索引对应值来进行一一比对 ...