练习11.2_人口数量_Python编程:从入门到实践(第3版)

修改前面的函数,使其包含第三个必不可少的形参population,并返回一个格式为City, Country -
population xxx 的字符串,如Santiago, Chile population 5000000。运行测试,确认test_city_country()未通过。

修改上述函数,将形参population设置为可选的。再次运行测试,确认test_city_country()又通过了。

再编写一个名为test_city_country_population()的测试,核实可以使用类似于’santiago’、 'chile’和’population=5000000’这样的值来调用这个函数。再次运行测试,确认test_city_country_population()通过了。

# city_country.py
def get_city_country(city,country,population=''):
    if population:
        city_country = f"{city},{country}-population {population}"
    else:
        city_country = f"{city},{country}"
    return city_country
#test_cities.py
from city_functions import get_city_country

def test_city_country():
    city_country = get_city_country('Santiago','Chile')
    assert city_country == "Santiago,Chile"
 
def test_city_country_population():
    city_country_population = get_city_country('Santiago','Chile',population=5000000)
    assert city_country_population == "Santiago,Chile-population 5000000"
(base) PS D:\desktop\python_work\11\chapter11> pytest
======================================================================================================= test session starts =======================================================================================================
platform win32 -- Python 3.12.4, pytest-7.4.4, pluggy-1.0.0
rootdir: D:\desktop\python_work\11\chapter11
plugins: anyio-4.2.0
collected 2 items

test_cities.py ..                                                                                                                                                                                                            [100%] 

======================================================================================================== 2 passed in 0.04s ======================================================================================================== 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值