python 单元测试 工具_单元测试 Python 代码 - Visual Studio | Microsoft Docs

选择适用于 Python 项目的测试框架Select the test framework for a Python project

Visual Studio 支持两种适用于 Python 的测试框架,即 unittest 和 pytest(从版本 16.3 开始,在 Visual Studio 2019 中可用)。Visual Studio supports two testing frameworks for Python, unittest and pytest (available in Visual Studio 2019 starting with version 16.3). 默认情况下,在创建 Python 项目时,不会选择任何框架。By default, no framework is selected when you create a Python project. 若要指定框架,在“解决方案资源管理器”中,右键单击“项目名称”,然后选择“属性”选项。To specify a framework, right-click on the project name in Solution Explorer and select the Properties option. 此操作将打开项目设计器,可用于通过“测试”选项卡配置测试。在此选项卡中,可以选择要用于项目的测试框架。This opens the project designer, which allows you to configure tests through the Test tab. From this tab, you can select the test framework that you want to use for your project.

对于 unittest 框架,项目的根目录用于测试发现。For the unittest framework, the project's root directory is used for test discovery. 可在“测试”选项卡上将此位置以及用于标识测试的文本模式修改为用户指定的值。This location, as well as the text pattern for identifying tests, can be modified on the Test tab to user specified values.

对于 pytest 框架,测试位置和文件名模式等测试选项是使用标准的 pytest .ini 配置文件指定的。For the pytest framework, testing options such as test location and filename patterns are specified using the standard pytest .ini configuration file. 有关详细信息,请参阅 pytest 参考文档。

保存框架选择和设置后,将在“测试资源管理器”中启动测试发现。Once you've saved your framework selection and settings, test discovery is initiated in the Test Explorer. 如果“测试资源管理器”窗口尚未打开,请导航至工具栏并选择“测试” > “测试资源管理器” 。If the Test Explorer window is not already open, navigate to the toolbar and select Test > Test Explorer.

在不使用项目的情况下针对 Python 配置测试Configure testing for Python without a project

使用 Visual Studio,可以在不使用项目的情况下运行和测试现有 Python 代码,方法是使用 Python 代码打开文件夹。Visual Studio allows you to run and test existing Python code without a project, by opening a folder with Python code. 在这些情况下,需要使用 PythonSettings.json 文件来配置测试。Under these circumstances, you'll need to use a PythonSettings.json file to configure testing.

使用“打开本地文件夹”选项来打开现有 Python 代码。Open your existing Python code using the Open a Local Folder option.

在“解决方案资源管理器”窗口中,单击“显示所有文件”图标以显示当前文件夹中的所有文件。Within the Solution Explorer window, click the Show All Files icon to show all files in the current folder.

导航到“本地设置”文件夹中的 PythonSettings.json 文件 。Navigate to the PythonSettings.json file within the Local Settings folder. 如果在“本地设置”文件夹中看不到此文件,请手动创建它。If you don't see this file in the Local Settings folder, create it manually.

将字段 TestFramework 添加到设置文件,并将其设置为 pytest 或 unittest,具体取决于要使用的测试框架 。Add the field TestFramework to the settings file and set it to pytest or unittest depending on the testing framework you want to use.

{

"TestFramework": "unittest",

"UnitTestRootDirectory": "testing",

"UnitTestPattern": "test_*.py"

}

备注

对于 unittest 框架,如果未在 PythonSettings.json 文件中指定字段 UnitTestRootDirectory 和 UnitTestPattern,则系统会添加它们并分别向其分配默认值“.”和“test*.py” 。For the unittest framework, if the fields UnitTestRootDirectory and UnitTestPattern are not specified in the PythonSettings.json file, they are added and assigned default values of "." and "test*.py" respectively.

如果你的文件夹包含 src 目录,该目录独立于包含测试的文件夹,则使用 PythonSettings.json 文件中的 SearchPaths 字段指定指向 src 文件夹的路径 。If your folder contains a src directory that is separate from the folder that contains your tests, specify the path to the src folder using the SearchPaths field in your PythonSettings.json file.

{

"TestFramework": "unittest",

"UnitTestRootDirectory": "testing",

"UnitTestPattern": "test_*.py",

"SearchPaths": [ ".\\src"]

}

保存对 PythonSettings.json 文件的更改,以启动指定框架的测试发现。Save your changes to the PythonSettings.json file to initiate test discovery for the specified framework.

备注

如果“测试资源管理器”窗口已打开,按 Ctrl + R,A 也会触发发现 。If the Test Explorer window is already open CTRL + R,A also triggers discovery.

发现和查看测试Discover and view tests

默认情况下,Visual Studio 将 unittest 和 pytest 测试标识为名称以“test”开头的方法 。By default, Visual Studio identifies unittest and pytest tests as methods whose names start with test. 若要查看测试发现,请执行以下操作:To see test discovery, do the following:

在 Visual Studio 中加载项目后,在“解决方案资源管理器”中,右键单击“项目”,然后从“属性测试”选项卡中选择 unittest 或 pytest 框架 。Once the project is loaded in Visual Studio, right-click your project in Solution Explorer and select the unittest or pytest framework from the Properties Test tab.

备注

如果使用 pytest 框架,可以使用标准的 pytest .ini 配置文件指定测试位置和文件名模式。If you use the pytest framework, you can specify test location and filename patterns using the standard pytest .ini configuration file. 默认情况下,使用工作区/项目文件夹,模式为 test_*py 和 *_test.py。By default, the workspace/project folder is used, with a pattern of test_*py and *_test.py. 有关详细信息,请参阅 pytest 参考文档。

选择框架后,再次右键单击项目,选择“添加” > “新建项”,然后依次选择“Python 单元测试”、“添加” 。After the framework is selected, right-click the project again and select Add > New Item, then select Python Unit Test followed by Add.

如果直接运行脚本,此操作将创建具有可导入标准 unittest 模块的代码的 test_1.py 文件,从 unittest.TestCase 派生一个测试类,并调用 unittest.main():This action creates a test_1.py file with code that imports the standard unittest module, derives a test class from unittest.TestCase, and invokes unittest.main() if you run the script directly:

import unittest

class Test_test1(unittest.TestCase):

def test_A(self):

self.fail("Not implemented")

if __name__ == '__main__':

unittest.main()

根据需要保存该文件,然后通过“测试” > “测试资源管理器”菜单命令打开“测试资源管理器” 。Save the file if necessary, then open Test Explorer with the Test > Test Explorer menu command.

“测试资源管理器”会搜索要测试的项目并进行显示,如下所示。Test Explorer searches your project for tests and displays them as shown below. 双击测试打开其源文件。Double-clicking a test opens its source file.

向项目添加更多测试时,可以使用工具栏上的“分组依据”菜单整理“测试资源管理器”中的视图 :As you add more tests to your project, you can organize the view in Test Explorer using the Group By menu on the toolbar:

还可以在“搜索”字段中输入文本以按名称筛选测试。You can also enter text in the Search field to filter tests by name.

有关 unittest 模块和编写测试的详细信息,请参阅 Python 2.7 文档或 Python 3.7 文档 (python.org)。For more information on the unittest module and writing tests, see the Python 2.7 documentation or the Python 3.7 documentation (python.org).

运行测试Run tests

在“测试资源管理器”中,可以通过多种方式来运行测试:In Test Explorer you can run tests in a variety of ways:

“运行所有”将运行所有显示的测试(取决于筛选器)。Run All clearly runs all shown tests (subject to filters).

“运行”菜单提供以下命令:运行失败的、通过的或未作为一组运行的测试。The Run menu gives you commands to run failed, passed, or not run tests as a group.

可以选择一个或多个测试,右键单击,然后选择“运行选定测试”。You can select one or more tests, right-click, and select Run Selected Tests.

测试在后台运行,每个测试完成后,“测试资源管理器”将更新其状态:Tests run in the background and Test Explorer updates each test's status as it completes:

通过的测试将显示一个绿勾和运行测试所花费的时间:Passing tests show a green tick and the time taken to run the test:

失败的测试将显示带有输出链接的红叉,该链接显示控制台输出和测试运行中的 unittest 输出:Failed tests show a red cross with an Output link that shows console output and unittest output from the test run:

调试测试Debug tests

因为单元测试是代码片段,所以和任何其他代码一样会受到 bug 的影响,有时需要在调试器中运行。Because unit tests are pieces of code, they are subject to bugs just like any other code and occasionally need to be run in a debugger. 在调试程序中,可以设置断点、检查变量和逐行执行代码。In the debugger you can set breakpoints, examine variables, and step through code. Visual Studio 还提供了用于单元测试的诊断工具。Visual Studio also provides diagnostic tools for unit tests.

备注

默认情况下,测试调试使用 Visual Studio 2017 的 ptvsd 4 调试器(版本 15.8 和更高版本)以及 Visual Studio 2019 的 debugpy(版本 16.5 和更高版本)。By default, test debugging uses the ptvsd 4 debugger for Visual Studio 2017 (versions 15.8 and later) and debugpy for Visual Studio 2019 (versions 16.5 and later). 如果要改用 ptvsd 3,可以在“工具” > “选项” > “Python” > “调试”中选择“使用旧版调试程序” 。If you would like to instead use ptvsd 3, you can select the Use Legacy Debugger option on Tools > Options > Python > Debugging.

若要开始调试,请在代码中设置初始断点,然后在“测试资源管理器”中右键单击测试(或所做选择),然后选择“调试所选测试”。To start debugging, set an initial breakpoint in your code, then right-click the test (or a selection) in Test Explorer and select Debug Selected Tests. Visual Studio 会启动 Python 调试程序,与为应用程序代码启动 Python 调试器一样。Visual Studio starts the Python debugger as it would for application code.

还可以使用“分析所选测试的代码覆盖率”。You can also use the Analyze Code Coverage for Selected Tests.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值