Bl4ckC4t
Bl4ckC4t

Reputation: 114

PyCharm doesn't appear to run all unit tests

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()

Upvotes: 0

Views: 4373

Answers (1)

M. K. Hunter
M. K. Hunter

Reputation: 1828

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.

Upvotes: 3

Related Questions