python学习笔记28(Pycharm使用unittest运行结果出现Launching unittests with arguments python -m unittest问题)

我用的是pycharm,刚学完单元测试,正准备大展拳脚做下面两道习题的时候,就出现了错误:

1.Pycharm使用unittest运行结果出现Launching unittests with arguments python -m unittest问题*

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()

单元测试

最后我想说一句,这个测试对于我这种神经大条的人而言简直是救星又是克星啊~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值