user5670001
user5670001

Reputation:

Unit testing in PyCharm

Alright, so I am doing Python Crash Course exercies for unit tests chapter and I can't quite get it to work using PyCharm.

My "project" literally consists of two trivial files:

city_functions.py:

def city_country(city, country):
    result = '{0}, {1}'.format(city.title(), country.title())
    return result

test_cities.py:

import unittest
from city_functions import city_country


class CityTestCase(unittest.TestCase):
    """Tests for 'city_functions.city_country' function."""

    def test_city_country(self):
        result = city_country('london', 'england')
        self.assertEqual(result, 'London, England')

unittest.main()

Now, when I am trying to run the module from PyCharm I get:

(...)
----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

but it works, when I run it from command line:

> python -m test_cities
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

Are there any shenanigans with PyCharm configuration to get it to work? I've read some posts about naming conventions (that test function and the whole module MUST start with 'test', but it already does in my case).

Upvotes: 0

Views: 4751

Answers (2)

Mia Olsen
Mia Olsen

Reputation: 1

I had exactly the same problem with PyCharm. I am following the Python Crash Course and followed the instructions step by step in chapter 11. And I also had to use:

if__name__ == '__main__':
  unittest.main()

to get the test working.

Upvotes: 0

user5670001
user5670001

Reputation:

Actually adding the main check solved the problem (it works from PyCharm and Command Line):

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

Upvotes: 2

Related Questions