修改前面的函数,使其包含第三个必不可少的形参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 ========================================================================================================