Practical Programming
An Introduction to Computer Science
Using Python 3
(Pragmatic Programmers)
(Pragmatic Bookshelf)
Paul Gries (著) Jennifer Campbell (著)
Jason Montojo (著) Lynn Beighley (編集)
開発環境
- OS X Yosemite - Apple (OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python 3.4 (プログラミング言語)
Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 15(Testing and Debugging)、15.7(Exercises) 4.を解いてみる。
15.7(Exercises) 4.
コード(BBEdit)
TestSorting.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import unittest
def isSorted(items):
return items == sorted(items)
class TestSorting(unittest.TestCase):
def test_empty(self):
items = []
expected = True
actual = isSorted(items)
self.assertEqual(expected, actual)
def test_one_element(self):
items = [10];
expected = True
actual = isSorted(items)
self.assertEqual(expected, actual)
def test_sorted_list(self):
items = [1, 2, 3, 4, 5]
expected = True
acual = isSorted(items)
self.assertEqual(expected, acual)
def test_have_deplicate_values(self):
items = [1, 2, 2, 3, 3, 4, 5]
expected = True
acual = isSorted(items)
self.assertEqual(expected, acual)
def test_false(self):
items = [2, 1]
expected = False
acual = isSorted(items)
self.assertEqual(expected, acual)
if __name__ == '__main__':
unittest.main()
入出力結果(Terminal, IPython)
$ ./TestSorting.py ..... ---------------------------------------------------------------------- Ran 5 tests in 0.003s OK $
0 コメント:
コメントを投稿