I have a test file and a main module file in which there's a function I'm testing. At the end of my test file, I have unittest.main()
to run the unit tests. However, When I run the test file, the console shows "No tests were found" , even though I have 2 unit tests in my file (see screenshot below and source code at the end).
This problem seems to go away when I:
(1) Enclose the unittest.main()
inside an if __name__ == "__main__"
(tangent: I sort of understand how this clause works, but it makes no sense for me in this case, when the unittest.main()
module runs properly when there's an if clause, versus when there's no coditional at all), OR
(2) When I run my test program in Spyder (I'm currently using Pycharm)
Therefore, I'm not quite sure this is an issue specific to my IDE or to my code. I've tried the recommended fix from this Q&A but none worked. If you have any idea on what I should do/configure to get unittest.main
running properly, I'd really appreciate it!
For your reference, here are the 2 files in my program; my test file returns no test as opposed to the 2 tests that I'd programmed for it.
---Main file: city_functions.py---
def print_city_country(city, country, population=""):
"""Print 'city, country' from input city and country"""
if population:
formatted_city_country = city + ", " + country + " - population " + str(population)
else:
formatted_city_country = city + ", " + country
return formatted_city_country
---Test file: test_cities.py---
import unittest
from city_functions import print_city_country
class TestCaseCityCountry(unittest.TestCase):
"""Test function city_country from city_functions module"""
def test_city_country_pair(self):
"""Test for names like Santiago, Chile without population input"""
formatted_city_country = print_city_country("Santiago", "Chile")
self.assertEqual(formatted_city_country, "Santiago, Chile")
def test_city_country_population(self):
"""Test for names like Santiago, Chile, 5000000"""
formatted_city_country_population = print_city_country("Santiago", "Chile", 5000000)
self.assertEqual(formatted_city_country_population, "Santiago, Chile - population 5000000")
unittest.main()