0

I have a problem running unittests in pycharm. The first class 'KnownValues' runs but the other class doesn't get checked at all.

import roman
import unittest

class KnownValues(unittest.TestCase):

    def test_too_large(self):
        '''to_roman should fail with large input'''
        self.assertRaises(roman.OutOfRangeError, roman.to_roman, 4000)
    def test_too_small(self):
        ls = [0,-1,-25,-60]
        for x in ls:
            self.assertRaises(roman.OutOfRangeError, roman.to_roman, x)
    def test_non_int(self):
        ls = [1.5, -6.5, 6.8,12.9, "hello wold", "nigga123"]
        for x in ls:
             self.assertRaises(roman.TypeError, roman.to_roman, x)

class Test2(unittest.TestCase):
    def test1(self):
        assert 1 == 1


if __name__ == '__main__':
    unittest.main()
holdenweb
  • 33,305
  • 7
  • 57
  • 77
Bl4ckC4t
  • 104
  • 1
  • 10
  • Have you verified that this differs from the behaviour when you run the program outside PyCharm? – holdenweb Apr 20 '17 at 07:49
  • What if you change the name of the test method from test1 to test_1? – Charlie Apr 20 '17 at 07:51
  • @Charlie no for pycharm it doesn't run it even if i change it to test_1 – Bl4ckC4t Apr 20 '17 at 08:01
  • @holdenweb yep when i run it from the cmd it does `test_non_int (__main__.KnownValues) ... ok test_to_roman_known_values (__main__.KnownValues) to_roman should give known result with known input ... ok test_too_large (__main__.KnownValues) to_roman should fail with large input ... ok test_too_small (__main__.KnownValues) ... ok **test_1 (__main__.Test2) ... ok** -------------------------------------------------------------------- Ran 5 tests in 0.000s` – Bl4ckC4t Apr 20 '17 at 08:03
  • I created a new project, pasted your code, removed the references to roman, and ran the tests and it ran them all. I right-clicked on the py file and clicked "Run unittests in [...]" – Charlie Apr 20 '17 at 08:12
  • 1
    Make sure you click the little green circle on the toolbar for the unit test viewer, by default it hides tests that pass. – Charlie Apr 20 '17 at 08:13
  • If this is the problem you should make it an answer - easier then for others to use it for reference. – holdenweb Apr 20 '17 at 08:21
  • @Charlie yeah it seems it works i was just running it with the 'Debug' not with the 'Run' – Bl4ckC4t Apr 20 '17 at 08:34
  • I just need to run the test file with the 'Run Unittests in test' and not the 'Debug Unittests in KnownValues' to work.(test is the filename of my unittest file 'test.py') – Bl4ckC4t Apr 20 '17 at 08:42

1 Answers1

3

Start all of your test functions with test. Many people use underscores to separate words, so a lot of people end up with tests starting with test_, but test is all that is required.

When having trouble in the GUI, you can check how your tests are running from the command line.

python test.py

or

python -m test

One problem that you might run into is that you have defined your tests within classes, and when running them through the GUI, the GUI has automatically discovered them for you. Be sure to include the lines at the end of your test file directing the interpreter to use the main function built into unittest.

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

Keep in mind, you can optionally run the tests in only one of your classes at a time:

python tests.py KnownValues
python tests.py Test2

In PyCharm, it should automatically discover all the test classes. You still have the option of running only one class at a time. Choose Run->Edit Configurations to see the options that you are currently running under. Using command line parameters you can control running fewer or more tests. enter image description here

As you can see, you can choose to run a script, a class, or a method. Be sure to set the name of your run configuration such that it reflects the scope of what you are running.

M. K. Hunter
  • 1,778
  • 3
  • 21
  • 27