用 Python 编写单元测试:我该如何开始?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3371255/
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:41:48  来源:igfitidea点击:

Writing unit tests in Python: How do I start?

pythonunit-testingtesting

提问by user225312

I completed my first proper project in Python and now my task is to write tests for it.

我用 Python 完成了我的第一个合适的项目,现在我的任务是为它编写测试。

Since this is the first time I did a project, this is the first time I would be writing tests for it.

由于这是我第一次做项目,这是我第一次为它编写测试。

The question is, howdo I start? I have absolutely no idea. Can anyone point me to some documentation/ tutorial/ link/ book that I can use to start with writing tests (and I guess unit testing in particular)

问题是,我该如何开始?我完全不知道。任何人都可以指点我一些文档/教程/链接/书籍,我可以用它来开始编写测试(我猜尤其是单元测试)

Any advice will be welcomed on this topic.

任何有关此主题的建议都将受到欢迎。

回答by Justin Ethier

The docs for unittestwould be a good place to start.

unittest的文档将是一个很好的起点。

Also, it is a bit late now, but in the future please consider writing unit tests before or during the project itself. That way you can use them to test as you go along, and (in theory) you can use them as regression tests, to verify that your code changes have not broken any existing code. This would give you the full benefit of writing test cases :)

此外,现在有点晚了,但将来请考虑在项目本身之前或期间编写单元测试。这样您就可以使用它们进行测试,并且(理论上)您可以将它们用作回归测试,以验证您的代码更改没有破坏任何现有代码。这将为您提供编写测试用例的全部好处:)

回答by Mark Byers

The free Python book Dive Into Pythonhas a chapter on unit testingthat you might find useful.

免费的 Python 书籍Dive Into Python有一章关于单元测试,您可能会觉得有用。

If you follow modern practices you should probably write the tests while you are writing your project, and not wait until your project is nearly finished.

如果您遵循现代实践,您可能应该在编写项目时编写测试,而不是等到项目快完成时。

Bit late now, but now you know for next time. :)

现在有点晚了,但现在你知道下次了。:)

回答by Tim McNamara

If you're brand new to using unittests, the simplest approach to learn is often the best. On that basis along I recommend using py.testrather than the default unittestmodule.

如果您是使用单元测试的新手,那么最简单的学习方法通​​常是最好的。在此基础上,我建议使用py.test而不是默认unittest模块

Consider these two examples, which do the same thing:

考虑这两个例子,它们做同样的事情:

Example 1 (unittest):

示例 1(单元测试):

import unittest

class LearningCase(unittest.TestCase):
    def test_starting_out(self):
        self.assertEqual(1, 1)

def main():
    unittest.main()

if __name__ == "__main__":
    main()

Example 2 (pytest):

示例 2(pytest):

def test_starting_out():
    assert 1 == 1

Assuming that both files are named test_unittesting.py, how do we run the tests?

假设两个文件都命名为test_unittesting.py,我们如何运行测试?

Example 1 (unittest):

示例 1(单元测试):

cd /path/to/dir/
python test_unittesting.py

Example 2 (pytest):

示例 2(pytest):

cd /path/to/dir/
py.test

回答by philant

As others already replied, it's late to write unit tests, but not too late. The question is whether your code is testableor not. Indeed, it's not easy to put existing code under test, there is even a book about this: Working Effectively with Legacy Code(see key pointsor precursor PDF).

正如其他人已经回答的那样,编写单元测试为时已晚,但还不算太晚。问题是您的代码是否可测试。事实上,测试现有代码并不容易,甚至有一本书是关于这个的:有效地使用遗留代码(参见关键点前身 PDF)。

Now writing the unit tests or not is your call. You just need to be aware that it could be a tedious task. You might tackle this to learn unit-testing or consider writing acceptance (end-to-end) tests first, and start writing unit tests when you'll change the code or add new feature to the project.

现在是否编写单元测试由您决定。您只需要意识到这可能是一项乏味的任务。您可以解决这个问题来学习单元测试或考虑首先编写验收(端到端)测试,然后在更改代码或向项目添加新功能时开始编写单元测试。

回答by Daniel Kluev

nosetests is brilliant solution for unit-testing in python. It supports both unittest based testcases and doctests, and gets you started with it with just simple config file.

鼻子测试是在 python 中进行单元测试的绝佳解决方案。它支持基于 unittest 的测试用例和 doctests,并让您通过简单的配置文件开始使用它。

回答by ssoler

unittestcomes with the standard library, but I would recomend you nosetests.

unittest随标准库一起提供,但我建议您使用nosetests

"nose extends unittest to make testing easier."

鼻子扩展了单元测试,使测试更容易。

I would also recomend you pylint

我也推荐你pylint

"analyzes Python source code looking for bugs and signs of poor quality."

"分析 Python 源代码,寻找错误和质量不佳的迹象。"

回答by Okken

There are, in my opinion, three great python testing frameworks that are good to check out.
unittest- module comes standard with all python distributions
nose- can run unittest tests, and has less boilerplate.
pytest- also runs unittest tests, has less boilerplate, better reporting, lots of cool extra features

在我看来,有三个很棒的 Python 测试框架值得一试。
单元测试-模块配备了所有的Python发行标准
鼻子-可以运行单元测试的测试,而且具有更少的样板。
pytest- 还运行单元测试测试,样板更少,报告更好,许多很酷的额外功能

To get a good comparison of all of these, read through the introductions to each at http://pythontesting.net/start-here.
There's also extended articles on fixtures, and more there.

要对所有这些进行比较,请通读http://pythontesting.net/start-here 上对每个的介绍。
还有关于固定装置的扩展文章,等等。