0% found this document useful (0 votes)
122 views1 page

Python Crash Course by Ehmatthes 11

This document contains code for testing functions that work with cities and countries, as well as code for an Employee class. It includes functions to return a city and country string, test those functions with unittest, modify the functions to accept an optional population parameter and add additional tests, and write a class with methods for initializing an employee and giving a raise along with tests for those methods. The code is tested incrementally and functions/tests are modified based on test outputs to resolve errors.

Uploaded by

alfonsofdez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views1 page

Python Crash Course by Ehmatthes 11

This document contains code for testing functions that work with cities and countries, as well as code for an Employee class. It includes functions to return a city and country string, test those functions with unittest, modify the functions to accept an optional population parameter and add additional tests, and write a class with methods for initializing an employee and giving a raise along with tests for those methods. The code is tested incrementally and functions/tests are modified based on test outputs to resolve errors.

Uploaded by

alfonsofdez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

!

"#$%&'()*+$'(%,)+-
!"#$%&'"#()$&(*+,-$.(/&0#-(/$%&#"(12#,("34,4$.56()&$7(8$(9,0&'-(*&"##:

!"#$%&'"#()$&(*+"(#"'$,-("-.*.$,(/&"(+"&"0(12-(3$4"(*$(5,$6(6+/*(7$%(*+.,5(/8$%*(97*+$,
:&/#+(:$%&#";(<3"/#"('$,#.-"&(*/5.,=(/(8&.")(#%&4"70(1)(7$%2-(3.5"(*$(5,$6(6+",(/--.*.$,/3
&"#$%&'"#(/&"(/4/.3/83">(7$%('/,(#.=,(%<()$&("?/.3(,$*.@'/*.$,#(+"&"0

!"#$%&"'()*)+,-.%/0)11
!!"!#$%&'()$%*+,'-(
!!".#$/*0+12'&*,
!!"3#$4501*(66

7289$'*$:*1+'&*,:;

!!"!#$%&'()$%*+,'-(
<-&'6$2$=+,8'&*,$'>2'$28860':$'?*$02-256'6-:#$2$8&'($,256$2,@$2$8*+,'-($,256;$A>6$=+,8'&*,
:>*+1@$-6'+-,$2$:&,B16$:'-&,B$*=$'>6$=*-5$!"#$%&!'()#*$)$:+8>$2:$ Santiago, Chile ;$C'*-6$'>6$=+,8'&*,
&,$2$5*@+16$82116@$+"#$,-()+#"')./0$;

%-62'6$2$D16$82116@$#1.#,+"#"1./0$$'>2'$'6:':$'>6$=+,8'&*,$(*+$E+:'$?-*'6$F-6565G6-$'>2'$(*+$,66@$'*
&50*-'$ unittest $2,@$'>6$=+,8'&*,$(*+$?2,'$'*$'6:'H;$<-&'6$2$56'>*@$82116@$ test_city_country() $'*
I6-&=($'>2'$8211&,B$(*+-$=+,8'&*,$?&'>$I21+6:$:+8>$2:$ santiago $2,@$ chile $-6:+1':$&,$'>6$8*--68'$:'-&,B;
J+,$#1.#,+"#"1./0$)$2,@$5296$:+-6$ test_city_country() $02::6:;

+"#$,-()+#"')./0$2

"""A collection of functions for working with cities."""

def city_country(city, country):


"""Return a string like 'Santiago, Chile'."""
return(city.title() + ", " + country.title())

!"#$%&34".&".&#41&-()+#"')&51&5*'#1&")&671*+".1&89:/

#1.#,+"#"1./0$2

import unittest

from city_functions import city_country

class CitiesTestCase(unittest.TestCase):
"""Tests for 'city_functions.py'."""

def test_city_country(self):
"""Does a simple city and country pair work?"""
santiago_chile = city_country('santiago', 'chile')
self.assertEqual(santiago_chile, 'Santiago, Chile')

unittest.main()

K+'0+'#

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

'*0

!!".#$/*0+12'&*,
L*@&=($(*+-$=+,8'&*,$:*$&'$-6M+&-6:$2$'>&-@$02-256'6-)$ population ;$N'$:>*+1@$,*?$-6'+-,$2$:&,B16
:'-&,B$*=$'>6$=*-5$ City, Country - population xxx )$:+8>$2:$ Santiago, Chile - population 5000000 ;
J+,$#1.#,+"#"1./0$$2B2&,;$L296$:+-6$ test_city_country() $=2&1:$'>&:$'&56;

L*@&=($'>6$=+,8'&*,$:*$'>6$ population $02-256'6-$&:$*0'&*,21;$J+,$#1.#,+"#"1./0$$2B2&,)$2,@$5296


:+-6$ test_city_country() $02::6:$2B2&,;

<-&'6$2$:68*,@$'6:'$82116@$ test_city_country_population() $'>2'$I6-&D6:$(*+$82,$8211$(*+-$=+,8'&*,


?&'>$'>6$I21+6:$ 'santiago' )$ 'chile' )$2,@$ 'population=5000000' ;$J+,$#1.#,+"#"1./0$$2B2&,)$2,@$5296
:+-6$'>&:$,6?$'6:'$02::6:;

L*@&D6@$+"#$,-()+#"')./0$)$?&'>$-6M+&-6@$ population $02-256'6-#

"""A collection of functions for working with cities."""

def city_country(city, country, population):


"""Return a string like 'Santiago, Chile - population 5000000'."""
output_string = city.title() + ", " + country.title()
output_string += ' - population ' + str(population)
return output_string

K+'0+'$=-*5$-+,,&,B$#1.#,+"#"1./0$2

E
======================================================================
ERROR: test_city_country (__main__.CitiesTestCase)
Does a simple city and country pair work?
----------------------------------------------------------------------
Traceback (most recent call last):
File "pcc\solutions\test_cities.py", line 10, in test_city_country
santiago_chile = city_country('santiago', 'chile')
TypeError: city_country() missing 1 required positional argument: 'population'

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

L*@&D6@$+"#$,-()+#"')./0$)$?&'>$*0'&*,21$ population $02-256'6-#

"""A collection of functions for working with cities."""

def city_country(city, country, population=0):


"""Return a string representing a city-country pair."""

output_string = city.title() + ", " + country.title()


if population:
output_string += ' - population ' + str(population)
return output_string

K+'0+'$*=$-+,,&,B$#1.#,+"#"1./0$2

.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

L*@&D6@$#1.#,+"#"1./0$2

import unittest

from city_functions import city_country

class CitiesTestCase(unittest.TestCase):
"""Tests for 'city_functions.py'."""

def test_city_country(self):
"""Does a simple city and country pair work?"""
santiago_chile = city_country('santiago', 'chile')
self.assertEqual(santiago_chile, 'Santiago, Chile')

def test_city_country_population(self):
"""Can I include a population argument?"""
santiago_chile = city_country('santiago', 'chile', population=5000000)
self.assertEqual(santiago_chile, 'Santiago, Chile - population 5000000')

unittest.main()

K+'0+'#

..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

'*0

!!"3#$4501*(66
<-&'6$2$812::$82116@$ Employee ;$A>6$ __init__() $56'>*@$:>*+1@$'296$&,$2$D-:'$,256)$2$12:'$,256)$2,@
2,$2,,+21$:212-()$2,@$:'*-6$628>$*=$'>6:6$2:$2''-&G+'6:;$<-&'6$2$56'>*@$82116@$ give_raise() $'>2'
2@@:$OPQQQ$'*$'>6$2,,+21$:212-($G($@6=2+1'$G+'$21:*$28860':$2$@&R6-6,'$-2&:6$25*+,';

<-&'6$2$'6:'$82:6$=*-$ Employee ;$<-&'6$'?*$'6:'$56'>*@:)$ test_give_default_raise() $2,@


test_give_custom_raise() ;$S:6$'>6$ setUp() $56'>*@$:*$(*+$@*,T'$>2I6$'*$8-62'6$2$,6?$6501*(66
&,:'2,86$&,$628>$'6:'$56'>*@;$J+,$(*+-$'6:'$82:6)$2,@$5296$:+-6$G*'>$'6:':$02::;

1;0<'$11/0$2

class Employee():
"""A class to represent an employee."""

def __init__(self, f_name, l_name, salary):


"""Initialize the employee."""
self.first = f_name.title()
self.last = l_name.title()
self.salary = salary

def give_raise(self, amount=5000):


"""Give the employee a raise."""
self.salary += amount

#1.#,1;0<'$11/0$2

import unittest

from employee import Employee

class TestEmployee(unittest.TestCase):
"""Tests for the module employee."""

def setUp(self):
"""Make an employee to use in tests."""
self.eric = Employee('eric', 'matthes', 65000)

def test_give_default_raise(self):
"""Test that a default raise works correctly."""
self.eric.give_raise()
self.assertEqual(self.eric.salary, 70000)

def test_give_custom_raise(self):
"""Test that a custom raise works correctly."""
self.eric.give_raise(10000)
self.assertEqual(self.eric.salary, 75000)

unittest.main()

K+'0+'#

..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

'*0

!"#$%&'()*+$'(%,)+-'.+'/*.&#*.&-0'1"'-$/*##$-+2
!"#$%&'()%*'$%()+),'-).%/0%1#-23/%4'()$%3$#+(%-")%5'06'+%-")6)%/0%7'$8+%98+(:

You might also like