【问题标题】:Simple unittest doesn't work in pycharm python简单的单元测试在 pycharm python 中不起作用
【发布时间】:2021-03-23 22:52:43
【问题描述】:

当我尝试在 pycharm 中进行简单的单元测试时,它不起作用,输出是“在 0.000 秒内运行 0 次测试”,而当我在 Geany 中做完全相同的事情时,它会运行,“运行 1 次测试0.000 秒”有人能找出原因吗?

import unittest

class Test(unittest.TestCase):
    def test_cos_tam(self):

        r = '200'
        self.assertEqual(r, '200')

unittest.main()
C:\Users\wycze\Desktop\python_work\WizualizacjaDanych\Scripts\python.exe "D:\pycharm\PyCharm Community Edition 2020.3.2\plugins\python-ce\helpers\pycharm\_jb_unittest_runner.py" --path C:/Users/wycze/Desktop/python_work/WizualizacjaDanych/test_python_repos.py
Testing started at 23:45 ...

----------------------------------------------------------------------
Launching unittests with arguments python -m unittest C:/Users/wycze/Desktop/python_work/WizualizacjaDanych/test_python_repos.py in C:\Users\wycze\Desktop\python_work\WizualizacjaDanych
Ran 0 tests in 0.000s


OK

Process finished with exit code 0

Empty suite

Empty suite

【问题讨论】:

  • 我的猜测是 Pycharm 的配置方式有问题。由于您没有向我们展示配置,因此很难告诉您更多信息。也就是说,您可能想尝试使用superuser.com,因为这不是关于您的代码的问题,而是关于您正在使用的程序(IDE)的问题。
  • @KarlKnechtel 哦,你可能是对的,我不知道这个网站。感谢您的回答
  • 直奔 python -m unittest path/to/file/test_python_repos.py 有效吗?
  • 我认为这与指定文件名与指定模块或类名有关。

标签: python unit-testing pycharm geany


【解决方案1】:

this answer 和我自己的一些测试代码的启发,我将文件夹重新格式化为:

C:\USERS\ME\DESKTOP\SO_TEST2
└───src
    └───test
            test_python_repos.py
            __init__.py

带有一个空的__init__.py 文件。我还在测试文件的最后一行添加了一个条件:

if __name__ == '__main__':
    unittest.main()

因为我认为您不希望它运行,除非您直接运行测试。我现在可以cd 进入src 目录并运行:

python -m unittest 结果如下:

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

希望对你有用...

【讨论】:

    猜你喜欢
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 2021-06-26
    • 2018-05-03
    相关资源
    最近更新 更多