简体   繁体   English

为什么要继承自unittest.TestCase?

[英]Why inherit from unittest.TestCase?

What's the motivation behind having my unit test classes inherit from unittest.TestCase , rather than object ? 使我的单元测试类从unittest.TestCase继承而不是对象继承的动机是什么? Does it matter if I'm using Nose (or PyTest) instead of unittest ? 如果我使用鼻子(或PyTest)而不是unittest吗?

If you don't inherit from unit.TestCase , the testing framework won't know that you want those classes to be test cases. 如果您不继承unit.TestCase ,那么测试框架将不会知道您希望这些类成为测试用例。 So when you try to run your tests, nothing will happen! 因此,当您尝试运行测试时,什么也不会发生!

Inheriting from unittest.TestCase lets you use method like: unittest.TestCase继承,您可以使用如下方法:

When you use pytest , you can write tests directly as functions (no need to subclass anything). 使用pytest时 ,您可以将测试直接作为函数编写(无需继承任何子类)。 You can achieve the same set up / tear down pattern by using fixtures , and write assertions using the assert statement since pytest features a powerful assertion introspection . 由于pytest具有强大的声明自省功能,因此您可以使用固定装置来实现相同的设置/拆除模式,并使用assert语句来编写声明。

nose also provide similar features (test functions with a with_setup decorator). 鼻子也提供类似的功能 (使用with_setup装饰器进行测试的功能)。

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

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