为什么我在 VS Code 中通过 Python 测试得到 "No tests discovered"?
Why am I getting "No tests discovered" with Python tests in VS Code?
这是我的前几行代码,但我已经编码了 20 年,所以我很快就想进行单元测试 运行。
我正在使用
- Windows 10
- 2019 年 1 月 7 日起的 VS Code 1.30.2。
- Python 3.7.2
- Python 扩展 ms-python.python 2018.12.1
这是我所在文件夹的内容。
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
据我所知,我不在"venv"。
这是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 Code 中发现测试时,我得到了。
No tests discovered, please check the configuration settings for the tests. Source: Python (Extension)
更有趣的是,运行 通过 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
正如在 documentation 中针对 unittest
模块所说的那样,您的测试方法名称需要以 test
.
开头
tests are defined with methods whose names start with the letters test. This naming convention informs the test runner about which methods represent tests.
例如
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...
请注意,其中 none 个实际上测试了您的 messing.py
功能。为此,您需要 import messing
,在其上调用函数,并断言那些函数 return 所期望的值。
最后,您应该遵循一些约定:
- 使用简短的测试名称
- 避免使用双下划线,因为它们通常引用 Python
中的 "magic" 变量
- 不要在每个测试中都引用您正在测试的内容,因为套件本身已经通过名称引用它
- 您的 class 名称不应包含下划线
这是我的前几行代码,但我已经编码了 20 年,所以我很快就想进行单元测试 运行。
我正在使用
- Windows 10
- 2019 年 1 月 7 日起的 VS Code 1.30.2。
- Python 3.7.2
- Python 扩展 ms-python.python 2018.12.1
这是我所在文件夹的内容。
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
据我所知,我不在"venv"。
这是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 Code 中发现测试时,我得到了。
No tests discovered, please check the configuration settings for the tests. Source: Python (Extension)
更有趣的是,运行 通过 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
正如在 documentation 中针对 unittest
模块所说的那样,您的测试方法名称需要以 test
.
tests are defined with methods whose names start with the letters test. This naming convention informs the test runner about which methods represent tests.
例如
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...
请注意,其中 none 个实际上测试了您的 messing.py
功能。为此,您需要 import messing
,在其上调用函数,并断言那些函数 return 所期望的值。
最后,您应该遵循一些约定:
- 使用简短的测试名称
- 避免使用双下划线,因为它们通常引用 Python 中的 "magic" 变量
- 不要在每个测试中都引用您正在测试的内容,因为套件本身已经通过名称引用它
- 您的 class 名称不应包含下划线