2

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?

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
RETOVSKEJ
  • 135
  • 1
  • 7
  • 1
    My guess is that there's something wrong with how Pycharm is configured. It's a little hard to tell you any more than that because you haven't shown us the configuration. That said, you might want to try on https://superuser.com instead because this isn't a question about your code, it's a question about programs you are using (the IDEs). – Karl Knechtel Mar 23 '21 at 22:55
  • @KarlKnechtel oh, you're probably right, I didn't know about this site. Thanks for answering – RETOVSKEJ Mar 23 '21 at 22:58
  • 1
    Does running straight `python -m unittest path/to/file/test_python_repos.py` work? – xdhmoore Mar 23 '21 at 22:59
  • This page looks relevant: https://docs.python.org/3/library/unittest.html#command-line-interface – xdhmoore Mar 23 '21 at 23:00
  • I think it has something to do with specifying a file name vs specifying a module or class name. – xdhmoore Mar 23 '21 at 23:02
  • @xdhmoore Looks like not ```C:\Users\wycze\Desktop\python_work\WizualizacjaDanych>python -m unittest test_python_repos.py E ====================================================================== ERROR: test_python_repos (unittest.loader._FailedTest) ---------------------------------------------------------------------- AttributeError: module '__main__' has no attribute 'test_python_repos' ---------------------------------------------------------------------- Ran 1 test in 0.000s FAILED (errors=1)``` – RETOVSKEJ Mar 23 '21 at 23:05
  • Sorry, I'm not sure. I've had similar issues in the past getting unit tests to work and I resolved them by making sure I was invoking the command from the right directory and with the right style of module name. Here it looks to me like it doesn't like the name `test_python_repos` by itself, but I'm not familiar enough with how the module names work to be more helpful. – xdhmoore Mar 23 '21 at 23:10
  • Another thing worth trying: `python -m unittest test_python_repos.Test` – xdhmoore Mar 23 '21 at 23:11
  • @xdhmoore i did many simple tests like this in Geany and most of them doesnt work in pycharm, I also changed the name of `test_python_repos` but still nothing. Thanks for trying – RETOVSKEJ Mar 23 '21 at 23:13

2 Answers2

1

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. 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:

python -m unittest with the following result:

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

OK

Hopefully that is of some use to you...

xdhmoore
  • 8,935
  • 11
  • 47
  • 90
0

I fixed a similar problem configuring Pycharm to run tests with

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

in your case you can set it to Unittest

magM
  • 131
  • 1
  • 8