我用的是pycharm,刚学完单元测试,正准备大展拳脚做下面两道习题的时候,就出现了错误:
1.Pycharm使用unittest运行结果出现Launching unittests with arguments python -m unittest问题*
我个人感觉是因为我这个文件下有两个test文件,然后python就搞混了,我灵机一动,那我就把另一个test文件删了吧,然后就还是这个界面。我就很迷,这时候就只能找外援了,终于顺利解决,而且我试过操作一次,后期再建立运行test文件不会再出现上述错误。
《Pycharm使用unittest运行结果出现Launching unittests with arguments python -m unittest问题*》
2.unittest习题
11-1 城市和国家:编写一个函数,它接受两个形参:一个城市名和一个国家名。这个函数返回一个格式为 City, Country 的字符串,如 Santiago, Chile。将这个函数存储在一个名为 city _functions.py 的模块中。创建一个名为test_cities.py 的程序,对刚编写的函数进行测试(别忘了,你需要导入模块 unittest 以及要测试的函数)。编写一个名为 test_city_country()的方法,核实使用类似于’santiago’和’chile’这样的值来调用前述函数时,得到的字符串是正确的。运行 test_cities.py,确认测试 test_city_country()通过了。
# city_functions.py文件内容
def city_country_info(city_name, country_name):
info = city_name + ',' + country_name
return info.title()
# test_cities.py文件内容
import unittest
from city_functions import city_country_info
class InfoTest(unittest.TestCase):
def test_city_country(self):
info = city_country_info('santiago', 'chile')
self.assertEqual(info, 'Santiago,Chile')
unittest.main()
11-2 人口数量:修改前面的函数,使其包含第三个必不可少的形参 population,并返回一个格式为 City, Country – population xxx 的字符串,如 Santiago, Chile – population 5000000。运行 test_cities.py,确认测试 test_city_country()未通过。修改上述函数,将形参 population 设置为可选的。再次运行 test_cities.py,确认测试 test_city_country()又通过了。再编写一个名为 test_city_country_population()的测试,核实可以使用类于’santiago’、'chile’和’population=5000000’这样的值来调用这个函数。再次运行test_cities.py,确认测试 test_city_country_population()通过了。
那我就把参数设置为可选的吧,然后就对了,代码如下:
def city_country_info(city_name, country_name, population=''):
info = city_name + ',' + country_name
total_info = info.title() + '--population' + str(population)
return total_info
import unittest
from city_functions import city_country_info
class InfoTest(unittest.TestCase):
def test_city_country(self):
info = city_country_info('santiago', 'chile')
self.assertEqual(info, 'Santiago,Chile--population')
unittest.main()
或者我又重新设置了一个测试函数:
def city_country_info(city_name, country_name, population=''):
info = city_name + ',' + country_name
total_info = info.title() + '--population ' + str(population)
return total_info
def city_country_info1(city_name, country_name, population):
info = city_name + ',' + country_name
total_info = info.title() + '--population ' + str(population)
return total_info
import unittest
from city_functions import city_country_info
from city_functions import city_country_info1
class InfoTest(unittest.TestCase):
def test_city_country(self):
info = city_country_info('santiago', 'chile')
self.assertEqual(info, 'Santiago,Chile--population ')
def test_city_country1(self):
info = city_country_info1('santiago', 'chile', '4000')
self.assertEqual(info, 'Santiago,Chile--population 4000')
unittest.main()
如果这里只是单纯的增加了一个测试函数,但是对于原函数而言人口这个形参还是必选项,也是照常出错,会提醒我们漏掉了一个参数
import unittest
from city_functions import city_country_info
class InfoTest(unittest.TestCase):
def test_city_country(self):
info = city_country_info('santiago', 'chile')
self.assertEqual(info, 'Santiago,Chile--population')
def test_city_country1(self):
info = city_country_info('santiago', 'chile', '4000')
self.assertEqual(info, 'Santiago,Chile--population 4000')
unittest.main()
最后我想说一句,这个测试对于我这种神经大条的人而言简直是救星又是克星啊~