Python 单元测试和发现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3295386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:18:24  来源:igfitidea点击:

Python unittest and discovery

pythonunit-testingdiscovery

提问by Aaron

I have directories, which contain files named like: test_foo.py

我有目录,其中包含名为如下的文件: test_foo.py

Each file is a test case.

每个文件都是一个测试用例。

I would like to

我想要

1) Run all the tests in a directory from the command line. I am using unittest2, since we are running Python 2.5.1. From one of these directories I tried typing this at the command line:

1)从命令行运行目录中的所有测试。我正在使用unittest2,因为我们运行的是 Python 2.5.1。从这些目录之一,我尝试在命令行中输入:

python -m unittest2 discover -p 'test_*.py'

and several different variants. I get no error, but nothing happens. I was expecting all the tests within all the test cases in that directory to run and get results.

和几种不同的变体。我没有收到错误,但没有任何反应。我期望该目录中所有测试用例中的所有测试都能运行并获得结果。

2) I also tried having a script in the directory where I did this:

2)我还尝试在我执行此操作的目录中有一个脚本:

loader = unittest2.TestLoader()
t = loader.discover('.')

If I print the tvariable, I can see my test cases, but from the docs I can't figure out what to do with the loader object once I have it.

如果我打印t变量,我可以看到我的测试用例,但是从文档中我无法弄清楚一旦我拥有加载器对象该怎么办。

回答by Alex Martelli

Give how you're trying to use unittest2from the command line on Python < 2.7, I think you may have missed the note on the unittest2PyPI page:

给出您如何尝试unittest2从命令行使用Python < 2.7,我想您可能错过unittest2PyPI 页面上的注释

Note

Command line usage

In Python 2.7 you invoke the unittest command line features (including test discover) with python -m unittest <args>. As unittest is a package, and the ability to invoke packages with python -m ...is new in Python 2.7, we can't do this for unittest2.

Instead unittest2 comes with a script unit2. Command line usage:

unit2 discover unit2 -v test_module

There is also a copy of this script called unit2.py, useful for Windows which uses file-extensions rather than shebang lines to determine what program to execute files with. Both of these scripts are installed by distutils.

笔记

命令行使用

在 Python 2.7 中,您可以使用python -m unittest <args>. 由于 unittest 是一个包,并且调用包的能力 python -m ...是 Python 2.7 中的新功能,因此我们无法为 unittest2 执行此操作。

相反,unittest2 带有一个脚本 unit2。命令行用法:

unit2 discover unit2 -v test_module

还有一个名为 的脚本副本unit2.py,对于 Windows 很有用,它使用文件扩展名而不是 shebang 行来确定用什么程序来执行文件。这两个脚本都是由 distutils 安装的。

Have you tried the unit2script which this note recommends as the alternative for older Pythons to the "run package as main script" feature of Python 2.7? Maybe its sources could also be useful to find out exactly how to discover-and-run tests from your own code, if that's what you'd rather do.

您是否尝试过unit2本说明推荐的脚本作为 Python 2.7 的“将包作为主脚本运行”功能的替代旧 Python 的替代方案?也许它的来源也可能有助于准确地找出如何从您自己的代码中发现和运行测试,如果这是您愿意的话。

回答by cmcginty

I ran into the same issue when running python -m unittest discover. Here is a good check list to verify your setup. Noseis more flexible with the allowed configurations, but not necessarily better.

我在运行时遇到了同样的问题python -m unittest discover。这是一个很好的检查清单来验证您的设置。Nose在允许的配置下更灵活,但不一定更好。

  1. Make sure all files/dirs start with test. Do notuse test-something.py, since that is not a valid python module name. Use test_something.py.

  2. If you are putting your tests in a sub-directory (e.g. test/), make sure you create a test/__init__.pyfile so python will treat the directory as a package.

  3. All class test cases definitions must be extend unittest.TestCase. For example

    class DataFormatTests(unittest.TestCase)
    
  1. 确保所有文件/目录都以test. 千万不能使用test-something.py,因为这不是一个有效的Python模块名称。使用test_something.py.

  2. 如果您将测试放在子目录(例如test/)中,请确保您创建了一个test/__init__.py文件,以便 python 将该目录视为一个包。

  3. 所有类测试用例定义都必须是 extend unittest.TestCase。例如

    class DataFormatTests(unittest.TestCase)
    

回答by kortina

Once you have discovered tests, you can run them with a test runner.

发现测试后,您可以使用测试运行器运行它们。

For python 2:

对于蟒蛇 2:

import unittest2
loader = unittest2.TestLoader()
tests = loader.discover('.')
testRunner = unittest2.runner.TextTestRunner()
testRunner.run(tests)

For python 3:

对于蟒蛇 3:

import unittest
loader = unittest.TestLoader()
tests = loader.discover('.')
testRunner = unittest.runner.TextTestRunner()
testRunner.run(tests)

Running the above code will print the test results to standard out.

运行上面的代码会将测试结果打印到标准输出。