Cucco’s Compute Hack

コンピュータ関係の記事を書いていきます。

pythonのunittestのコード

ちょっと前に書いた記事compute-cucco.hatenablog.comのテストコード。というか抽象クラスは関係ないので、ただのテストコード。
raiseに対するテストコードの書き方が分からない。。。

# -*- coding: utf-8 -*-
import unittest
import movingCalcs
from movingCalcs import factory

class TestmovingCalcs(unittest.TestCase):
	"""test class of movingCalcs.py
	"""

	def test_add(self):
		"""test method for Add
		"""
		node = factory("Add")
		value1 = 1
		value2 = 2
		expected = 3
		actual = node.calc(value1, value2)
		self.assertEqual(expected, actual)

	def test_sub(self):
		"""test method for sub
		"""
		node = factory("Sub")
		value1 = 1
		value2 = 2
		expected = -1
		actual = node.calc(value1, value2)
		self.assertEqual(expected, actual)

	def test_zero(self):
		"""test method for Zero
		"""
		node = factory("Zero")
		value1 = 1
		value2 = 2
		expected = 0
		actual = node.calc(value1, value2)
		self.assertEqual(expected, actual)

	def test_other(self):
		"""test method for other
		"""
		with self.assertRaises(factory):
			node = factory("Other")

if __name__ == "__main__":
    unittest.main()