1

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). No tests were found Pycharm 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()
seismatica
  • 437
  • 1
  • 6
  • 19
  • How do you run the tests in Pycharm? – quamrana Oct 05 '17 at 11:21
  • Perfect question! I used Ctrl + Shift + F10 (like this [tutorial](https://confluence.jetbrains.com/display/PYH/Creating+and+running+a+Python+unit+test) showed), but I just went back and used Alt + Shift + F10 (the generic run hotkey for Pycharm), it worked (though without the fancy Pycharm test status you see on the left of my screenshot, just the console result). Do you know why there's such difference? I looked up Ctrl + Shift + 10 but found little reference for it, just cheatsheets saying it stands for "Run context configuration from editor", whatever that means. – seismatica Oct 05 '17 at 11:40
  • I separate my application files and tests into two different sibling folders. Then I can right click on the test folder and select `Run Notestests in tests`. (where tests is the name of my test folder). – quamrana Oct 05 '17 at 11:55
  • Random guess: There's some logic in Pycharm that only collects and runs tests which have not yet been run by using some (implicit or explicit) filter. When you run it via Pycharm like you have done, it already runs all tests upon importing the module, and only then does Pycharm get a chance to execute them once more after collecting them. – Yam Marcovic Oct 05 '17 at 12:22
  • 1
    By the way, strictly speaking, you shouldn't include `unittest.main()` at all in your file. Just the tests themselves, and then you run the file like so: `python -m unittest myfile.py` – Yam Marcovic Oct 05 '17 at 12:23
  • @YamMarcovic: Yes, all my test files just have the test classes in them. Nosetests manages to find all the tests by itself given the folder they reside in. – quamrana Oct 05 '17 at 12:59
  • 1
    @YamMarcovic thanks for the tip! Someone at Pycharm also answered my question with a similar idea, and explain the difference between `Alt + Shift + F10` and `Ctrl + Shift + F10` (https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000635410--No-tests-were-found-when-running-unittest-main-) – seismatica Oct 05 '17 at 23:54

3 Answers3

4

As a beginner using Pycharm, Don Kirkby's answer helped me the most.

The solution, at least for me was to simply remove unittest.main() from the file altogether. It appears that Pycharm is using the command python -m unittest by default whenever the test is run and unittest.main() method is messing up the syntax.

I hope this helps anyone else who came here with the same problem.

0

There are two ways to launch unittest tests. One is with the unittest.main() method that you're using, but I always use the command-line interface instead. The python -m unittest discover command will find tests in a bunch of files and run them all together.

I'm not sure why unittest.main() isn't working for you, but I would suggest using the other method anyway.

Don Kirkby
  • 53,582
  • 27
  • 205
  • 286
0

i had the same issue, and find out that if you put var = unittest.main instead unittest.main(), it's work. The Pycharm's tips tell me that.

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 06 '22 at 09:25