问题:用Python编写单元测试:如何开始?[关闭]

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

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

问题是,我该如何开始?我真的一点儿都不知道。谁能指出我一些可以用来开始编写测试的文档/教程/链接/书(尤其是单元测试)

关于该主题的任何建议都将受到欢迎。

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

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, how do 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.


回答 0

如果您是第一次使用单元测试,那么最简单的学习方法通​​常是最好的。在此基础上,我建议使用py.test而不是默认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()

示例2(pytest):

def test_starting_out():
    assert 1 == 1

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

示例1(单元测试):

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

示例2(pytest):

cd /path/to/dir/
py.test

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.test rather than the default unittest module.

Consider these two examples, which do the same thing:

Example 1 (unittest):

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):

def test_starting_out():
    assert 1 == 1

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

Example 1 (unittest):

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

Example 2 (pytest):

cd /path/to/dir/
py.test

回答 1

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

如果您遵循现代实践,则可能应该在编写项目时编写测试,而不要等到项目接近完成时再进行测试。

现在有点晚,但是现在您知道下一次了。:)

The free Python book Dive Into Python has a chapter on unit testing that you might find useful.

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. :)


回答 2

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

为了对所有这些进行良好的比较,请阅读http://pythontesting.net/start-here的每个介绍。
也有关于灯具的扩展文章,还有更多。

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

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.


回答 3

的文档 单元测试将是一个良好的开端。

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

The docs for unittest would be a good place to start.

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 :)


回答 4

unittest随标准库一起提供,但是我建议您进行鼻子测试

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

我也建议你pylint

分析Python源代码以寻找错误和质量低劣的迹象。

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

nose extends unittest to make testing easier.

I would also recomend you pylint

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


回答 5

正如其他人已经回答的那样,编写单元测试已经晚了,但还不算太晚。问题是您的代码是否可测试。确实,要对现有代码进行测试并不容易,甚至有一本关于此的书:有效地使用遗留代码(请参见要点前期PDF)。

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

As others already replied, it’s late to write unit tests, but not too late. The question is whether your code is testable or 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 points or precursor 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.


回答 6

鼻子测试是在python中进行单元测试的出色解决方案。它同时支持基于单元测试的测试用例和doctest,并且只需简单的配置文件就可以开始使用它。

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.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。