11-1:
import unittest def city_function(city,country): return city.title()+', '+country.title() class TestCities(unittest.TestCase): def test_city_country(self): format_ans=city_function('shenzhen','china') self.assertEqual(format_ans,'Shenzhen, China') unittest.main()
本题输出如下:
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK
11-2:
population为必选参数时:
import unittest def city_function(city,country,population): return city.title()+', '+country.title()+' - population '+population class TestCities(unittest.TestCase): def test_city_country(self): format_ans=city_function('shenzhen','china') self.assertEqual(format_ans,'Shenzhen, China') unittest.main()
输出为:
E ====================================================================== ERROR: test_city_country (__main__.TestCities) ---------------------------------------------------------------------- Traceback (most recent call last): File ".\hw week6.py", line 7, in test_city_country format_ans=city_function('shenzhen','china') TypeError: city_function() missing 1 required positional argument: 'population' ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (errors=1)
population为可选参数时:
import unittest def city_function(city,country,population=0): if population: return city.title()+', '+country.title()+' - population '+str(population) else: return city.title()+', '+country.title() class TestCities(unittest.TestCase): def test_city_country(self): format_ans=city_function('shenzhen','china') self.assertEqual(format_ans,'Shenzhen, China') unittest.main()
输出为:
. ---------------------------------------------------------------------- Ran 1 test in 0.000s OK
再添加一个测试函数后:
import unittest def city_function(city,country,population=0): if population: return city.title()+', '+country.title()+' - population '+str(population) else: return city.title()+', '+country.title() class TestCities(unittest.TestCase): def test_city_country(self): format_ans=city_function('shenzhen','china') self.assertEqual(format_ans,'Shenzhen, China') def test_city_country_population(self): format_ans=city_function('shenzhen','china',12528300) self.assertEqual(format_ans,'Shenzhen, China - population 12528300') unittest.main()
输出为:
.. ---------------------------------------------------------------------- Ran 2 tests in 0.000s OK
11-3:
class Employee: def __init__ (self,firstname,lastname,salary_for_year=0): self.firstname=firstname self.lastname=lastname self.salary_for_year=salary_for_year def give_raise(self,salary_raise=5000): self.salary_for_year=salary_raise import unittest class Test(unittest.TestCase): def setUp(self): self.salary=10000 self.employee=Employee('Taylor','Green') def test_give_default_raise(self): self.employee.give_raise() self.assertEqual(5000,self.employee.salary_for_year) def test_give_custom_raise(self): self.employee.give_raise(self.salary) self.assertEqual(10000,self.employee.salary_for_year) unittest.main()
输出如下:
.. ---------------------------------------------------------------------- Ran 2 tests in 0.000s OK