这是我的第几行代码,但我已经编码了20年,所以我很快就想让单元测试运行。
我在用
这是我所在文件夹的内容。
Directory: C:\DATA\Git\Py\my_first_code
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 19/01/2019 21:42 __pycache__
-a---- 19/01/2019 21:35 289 messing.py
-a---- 19/01/2019 21:42 204 test_messing.py
-a---- 19/01/2019 22:07 0 __init__.py据我所知,我不是“复仇”的。
这是test_messing.py的内容。
import unittest
class Test_Math(unittest.TestCase):
def math_multiply__when__2_times_2__then__equals_4(self):
self.assertEqual(2 * 2, 4)
if __name__ == '__main__':
unittest.main()__init__.py是空的,我添加它是为了看看它是否有用,而messing.py包含了一本书中的8行代码。
当我试图发现VS代码中的测试时,我会得到。
未发现测试,请检查测试的配置设置。资料来源: Python (扩展名)
更有趣的是,通过Python命令行运行测试发现如下所示。
PS C:\DATA\Git\Py\my_first_code> python -m unittest discover -v -s . -p test_*.py
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK发布于 2019-01-19 22:32:20
正如文档在unittest模块中所说的那样,您的测试方法名称需要从test开始。
测试由名称以字母测试开头的方法定义。此命名约定通知测试运行程序哪些方法代表测试。
例如
class TestMath(unittest.TestCase):
def test_multiply(self):
self.assertEqual(2 * 2, 4)
def test_multiply_negative(self):
self.assertEqual(-2 * -2, 4)
self.assertEqual(2 * -2, -4)
# etc...请注意,这些都没有实际测试您的messing.py功能。为了做到这一点,您需要调用import messing上的函数,并断言那些函数返回的值是预期的。
最后,您应该遵循一些约定:
https://stackoverflow.com/questions/54271900
复制相似问题