简体   繁体   English

如何使用Atom编辑器运行Python单元测试?

[英]How to run a Python unit test with the Atom editor?

我正在尝试使用Atom编辑器,并想知道如何使用键盘快捷键运行Python单元测试。

Installation 安装

  1. Install the Atom editor 安装Atom编辑器
  2. Install the Script package like this: 像这样安装脚本包:

    a) Start Atom a)启动Atom

    b) Press Ctrl + Shift + P , type "install packages and themes" and press Enter to open the package view b)按Ctrl + Shift + P ,键入“安装包和主题”,然后按Enter打开包视图

    c) Search for "script" and install the package c)搜索“脚本”并安装包

Unit test example test.py 单元测试示例test.py

  1. Write a unit test and save it as test.py . 编写单元测试并将其另存为test.py

     import unittest class MyTest(unittest.TestCase): def test_pass(self): pass def test_fail(self): call_method_that_does_not_exist() if __name__ == '__main__': unittest.main() 

Run unit test 运行单元测试

  1. Now, press Ctrl + I to run the Python script ( see documentation ) 现在,按Ctrl + I运行Python脚本( 参见文档

Console output 控制台输出

Because the unit test test_fail will fail, this will be the console output: 因为单元测试test_fail将失败,这将是控制台输出:

E.
======================================================================
ERROR: test_fail (__main__.MyTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/Lernkurve/Desktop/PythonDemos/a.py", line 9, in test_fail
    call_method_that_does_not_exist()
NameError: global name 'call_method_that_does_not_exist' is not defined

----------------------------------------------------------------------
Ran 2 tests in 0.000s

FAILED (errors=1)
[Finished in 0.047s]

You could use the Atom Python Test plug-in. 您可以使用Atom Python Test插件。 It supports: 它支持:

  • Run the test under cursor 在光标下运行测试
  • Run all tests of a module 运行模块的所有测试
  • Run doc tests 运行doc测试

It also supports adding additional arguments to test execution and allows to run unitttest.TestCase too. 它还支持添加额外的参数来测试执行,并允许运行unitttest.TestCase。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM