iPython notebook用unittest


In [3]:
# -*- coding: utf-8 -*-
from __future__ import division, print_function
import numpy as np
import unittest
from sample import MyStrategy, AllC, AllD, GrimTrigger, Alternate, RandomStrategy

In [5]:
class TestStrategies(unittest.TestCase):
    def setUp(self):
        self.Strategies = [MyStrategy, AllC, AllD, GrimTrigger, Alternate, RandomStrategy] # ここに自作のclassを入れる
        self.case1 = "Signal is empty(period 1)"
        self.case2 = [0, 1]
        self.case3 = [1, 0]
        self.case4 = [0, 1, 0, 1, 0, 0, 1]

        self.seed = 11451
        self.RandomState = np.random.RandomState(self.seed)


    # case1を引数に渡してテスト
    def test1(self):
        print("testcase:", self.case1)
        for Strategy in self.Strategies:
            rst = Strategy(self.RandomState).play()
            self.assertIn(rst, (0, 1))


    # case2を引数に渡してテスト
    def test2(self):
        print("testcase:", self.case2)
        for Strategy in self.Strategies:
            S = Strategy(self.RandomState)
            S.get_signal(self.case2)
            rst = S.play()
            self.assertIn(rst, (0, 1))


    # case3を引数に渡してテスト
    def test3(self):
        print("testcase:", self.case3)
        for Strategy in self.Strategies:
            S = Strategy(self.RandomState)
            for signal in self.case3:
                S.get_signal(signal)
            
            rst = S.play()
            self.assertIn(rst, (0, 1))


    # case4を引数に渡してテスト
    def test4(self):
        print("testcase:", self.case4)
        for Strategy in self.Strategies:
            S = Strategy(self.RandomState)
            for signal in self.case4:
                S.get_signal(signal)
                rst = S.play()
            
            self.assertIn(rst, (0, 1))

In [8]:
suite = unittest.TestLoader().loadTestsFromTestCase(TestStrategies)
unittest.TextTestRunner().run(suite)


....
testcase: Signal is empty(period 1)
testcase: [0, 1]
testcase: [1, 0]
testcase: [0, 1, 0, 1, 0, 0, 1]
----------------------------------------------------------------------
Ran 4 tests in 0.003s

OK
Out[8]:
<unittest.runner.TextTestResult run=4 errors=0 failures=0>

In [ ]: