python作业06代码测试

Description (test)

Select from exercise 11-1 to 11-3

Code (test)

11-1

in city_functions.py:

1
2
def city_functions( city, country):
return city+', '+country

in test module:

1
2
3
4
5
6
7
8
9
10
11
12
13
# 11-1 城市和国家 :编写一个函数,它接受两个形参:一个城市名和一个国家名。这个函数返回一个格式为City, Country 的字符串,
# 如Santiago, Chile 。将这个函数存储在一个名为city_functions.py的模块中。创建一个名为test_cities.py的程序,对刚编写的函
# 数进行测试(别忘了,你需要导入模块unittest 以及要测试的函数)。编写一个名为test_city_country() 的方法,核实使用类似于
# 'santiago' 和'chile' 这样的值来调用前述函数时,得到的字符串是正确的。运行test_cities.py ,确认测试test_city_country() 通过了。

import unittest
from city_functions import city_functions

class CityCountryTestCase( unittest.TestCase):

def test_city_functions(self):
res = city_functions('Santiago', 'Chile')
self.assertEqual(res, 'Santiago, Chile')

result:

1
2
3
4
5
6
7
8
Testing started at 13:15 ...
D:\pyproject\homework1\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2017.3.3\helpers\pycharm\_jb_unittest_runner.py" --path D:/pyproject/homework1/11-1.py


Ran 1 test in 0.001s

OK
Launching unittests with arguments python -m unittest D:/pyproject/homework1/11-1.py in D:\pyproject\homework1

11-2

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

in city_functions.py:

1
2
def city_functions( city, country, population):
return city + ', ' + country + ' - population ' + str(population)

in test module:

1
2
3
4
5
6
7
8
import unittest
from city_functions import city_functions

class CityCountryTestCase( unittest.TestCase):

def test_city_functions(self):
res = city_functions('Santiago', 'Chile')
self.assertEqual(res, 'Santiago, Chile')

result:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Testing started at 13:25 ...
D:\pyproject\homework1\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2017.3.3\helpers\pycharm\_jb_unittest_runner.py" --target 11-2.CityCountryTestCase.test_city_functions
Launching unittests with arguments python -m unittest 11-2.CityCountryTestCase.test_city_functions in D:\pyproject\homework1


Ran 1 test in 0.004s

FAILED (errors=1)

Error
Traceback (most recent call last):
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 59, in testPartExecutor
yield
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 601, in run
testMethod()
File "D:\pyproject\homework1\11-2.py", line 12, in test_city_functions
res = city_functions('Santiago', 'Chile')
TypeError: city_functions() missing 1 required positional argument: 'population'


Process finished with exit code 1

  • 修改上述函数,将形参population 设置为可选的。再次运行test_cities.py,确认测试test_city_country() 又通过了。
    再编写一个名为test_city_country_population() 的测试,核实可以使用类似于'santiago''chile''population=5000000' 这样的值来调用
    这个函数。再次运行test_cities.py,确认测试test_city_country_population() 通过了。

in city_functions.py:

1
2
3
4
5
def city_functions( city, country, population=-1):
if population == -1:
return city + ', ' + country
else:
return city + ', ' + country + ' - population ' + str(population)

in test module:

1
2
3
4
5
6
7
8
9
10
11
12
import unittest
from city_functions import city_functions

class CityCountryTestCase( unittest.TestCase):

def test_city_functions(self):
res = city_functions('Santiago', 'Chile')
self.assertEqual(res, 'Santiago, Chile')

def test_city_functions_population(self):
res = city_functions('Santiago', 'Chile', 500000)
self.assertEqual(res, 'Santiago, Chile - population 500000')

result:

1
2
3
4
5
6
7
8
9
10
Testing started at 13:34 ...
D:\pyproject\homework1\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2017.3.3\helpers\pycharm\_jb_unittest_runner.py" --path D:/pyproject/homework1/11-2.py
Launching unittests with arguments python -m unittest D:/pyproject/homework1/11-2.py in D:\pyproject\homework1


Ran 2 tests in 0.001s

OK

Process finished with exit code 0