2017年1月22日日曜日

開発環境

Python Crash Course (Eric Matthes (著)、No Starch Press)のPART1(BASICS)、Chapter 11.(TESTING YOUR CODE)のTRY IT YOURSELF 11-3.(Employee)(No.6401)を取り組んでみる。

TRY IT YOURSELF 11-3.(Employee)(No.6401)

Unit Test

コード(Emacs)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import unittest

from employee import *


class EmployeeTest(unittest.TestCase):

    def setUp(self):
        self.salary = 100000
        self.employee = Employee('first_name', 'last_name', self.salary)

    def tearDown(self):
        pass

    def test_give_default_raise(self):
        self.employee.give_raise()
        self.assertEqual(self.employee.salary, self.salary + 5000)

    def test_give_custom_raise(self):
        add_salary = 10000
        self.employee.give_raise(add_salary)
        self.assertEqual(self.employee.salary, self.salary + add_salary)

if __name__ == '__main__':
    unittest.main()
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


class Employee:

    def __init__(self, first_name, last_name, salary):
        self.first_name = first_name
        self.last_name = last_name
        self.salary = salary

    def give_raise(self, add_salary=5000):
        self.salary += add_salary

入出力結果(Terminal, IPython)

$ ./test_employee.py -v
test_give_custom_raise (__main__.EmployeeTest) ... ok
test_give_default_raise (__main__.EmployeeTest) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.002s

OK
$

0 コメント:

コメントを投稿