简单单元测试在pycharm python中不起作用

fhity93d  于 2023-01-19  发布在  Python
关注(0)|答案(2)|浏览(168)

当我尝试在pycharm中进行简单的单元测试时,它不工作,输出是"在0.000s内运行0次测试",当我在Geany中进行完全相同的操作时,它工作,"在0.000s内运行1次测试",有人能找出原因吗?

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
rwqw0loc

rwqw0loc1#

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

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

我还在测试文件的最后一行添加了一个条件:

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

因为我认为除非直接运行测试,否则您不希望运行它。现在我可以将cd导入src目录并运行:
python -m unittest,结果如下:

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

OK

希望这对你有用...

7gyucuyw

7gyucuyw2#

我修复了一个类似的问题,配置Pycharm来运行测试
Preferences/Tools/Python Integrated Tools/Testing/Default test runner = pytest.
在您情况下,可以将其设置为Unittest

相关问题