简体   繁体   中英

Python unittest returns “Ran 0 tests in 0.000s” and I have no idea why

I'm stuck with this problem and I have no idea why this test doesn't work. Please anyone help me.

Code below is in the cityname.py file

def get_name(city, country):
    return (city.title() + ", " + country.title())

Code below is in the test_cities.py file

import unittest

from cityname import get_name

class CitiesTestCase(unittest.TestCase):

    def test_city_country(self):
        santiago_chile = get_name('santiago', 'chile')
        self.assertEqual(santiago_chile, 'Santiago, Chile')

unittest.main()

Here is the output

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK
  1. Either remove unittest.main()
  2. Or pass the argument exit=False , unittest.main(exit=False)
  3. Or surround unittest.main( ) with this if statment

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

Your can read more about how this works in the Python docs...https://docs.python.org/3.6/library/unittest.html#unittest.main

In my case, it's that I have both python2 and python3 installed, and "python" refers to py2. "python -m unittest -v" always return "Ran 0 test" which is fixed by "python3 -m unittest -v"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM