简体   繁体   English

Python 单元测试和断言方法

[英]Python unittest and assert method

I've been reading online that Python's unittest does not include the assert command, and you can only get it from pytest.我一直在网上看,Python 的 unittest 不包含 assert 命令,只能从 pytest 中获取。 And, that with unittest you can only use the methods like assertEqual and assertTrue, etc... However, I have been able to use assert all by itself in unittest without importing pytest with only an import unittest line at the top of my code.而且,对于 unittest,您只能使用 assertEqual 和 assertTrue 等方法……但是,我已经能够在 unittest 中单独使用 assert all ,而无需在代码顶部仅导入一个 import unittest 行来导入 pytest。

assert x==1断言 x==1

This works fine.这工作正常。 Has unittest now incorporated this into its own framework? unittest 现在是否已将其合并到自己的框架中? I have not see it in the docs.我没有在文档中看到它。 I had previously installed pytest on my machine, but am not importing it directly into the module.我以前在我的机器上安装了 pytest,但没有将它直接导入模块。 Is it that it's somehow picking up the pytest methods just with the import unittest line?是不是它只是通过 import unittest 行以某种方式获取了 pytest 方法?

Thanks.谢谢。

I've been reading online that Python's unittest does not include the assert command, and you can only get it from pytest.我一直在网上看,Python 的 unittest 不包含 assert 命令,只能从 pytest 中获取。

assert is part of the language. assert是语言的一部分。 You don't have to get it from anywhere.您不必从任何地方获取它。

$ python3
Python 3.8.1 (default, Apr  4 2020, 07:35:43) 
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> assert True==False
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError
>>> 

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

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