简体   繁体   English

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

[英]Simple unittest doesn't work in pycharm python

When I try to do simple unittest in pycharm, It doesn't work, the output is "Ran 0 Tests in 0.000s" and when I'm doing exactly same thing in Geany, it works, "Ran 1 Test in 0.000s" can someone figure out why?当我尝试在 pycharm 中进行简单的单元测试时,它不起作用,output 是“在 0.000 秒内进行 0 次测试”,而当我在 Geany 中做完全相同的事情时,它有效,“在 0.000 秒内进行 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

Inspired by this answer and some of my own test code I reformatted the folders to be:受此答案和我自己的一些测试代码的启发,我将文件夹重新格式化为:

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

with an empty __init__.py file.带有一个空的__init__.py文件。 I also added a conditional on the last line of your test file:我还在测试文件的最后一行添加了一个条件:

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

because I don't think you want that to run unless you run the tests directly.因为除非您直接运行测试,否则我认为您不希望它运行。 I'm now able to cd into the src directory and run:我现在可以cd进入src目录并运行:

python -m unittest with the following result: python -m unittest结果如下:

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

OK

Hopefully that is of some use to you...希望这对你有点用...

I fixed a similar problem configuring Pycharm to run tests with我修复了配置 Pycharm 以运行测试的类似问题

Preferences/Tools/Python Integrated Tools/Testing/Default test runner = pytest.

in your case you can set it to Unittest在您的情况下,您可以将其设置为Unittest

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

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