Exemplo n.º 1
0
    def test_calculation_salary_patch_side_effect(self):
        def f(year):
            return 1
        # self.mock_bonus.side_effect = lambda year: 1
        # self.mock_bonus.side_effect = ConnectionResetError
        self.mock_bonus.side_effect =\
            [
                1,
                2,
                3,
                ValueError('Bankrupt!!')
            ]

        s = salary.Salary(year=2017)
        salary_price = s.calculation_salary()
        self.assertEqual(salary_price, 101)

        s = salary.Salary(year=2017)
        salary_price = s.calculation_salary()
        self.assertEqual(salary_price, 102)

        s = salary.Salary(year=2017)
        salary_price = s.calculation_salary()
        self.assertEqual(salary_price, 103)

        s = salary.Salary(year=200)
        with self.assertRaises(ValueError):
            s.calculation_salary()
    def test_patch_object_method(self, mocker, prepare):
        """インスタンスメンバのメソッドをモック化"""
        s = salary.Salary()
        mocker.patch.object(s, 'calcuration_heavy_task', return_value=500)

        result = s.calcuration_bonus()
        assert result == 600
 def test_calculation_salary_no_salary(self, mocker, prepare):
     s = salary.Salary(year=2030)
     s.bonus_api.bonus_price = mocker.patch(
         'salary.ThirdPartryBonusRestAPi.bonus_price',
         side_effect=dummy.dummy_bonus_price)
     assert s.calcuration_salary() == 100
     s.bonus_api.bonus_price.assert_not_called()
Exemplo n.º 4
0
 def test_calculation_salary_patch_with(self):
     with mock.patch(
             "salary.ThirdPartyBonusRestApi.bonus_price") as mock_bonus:
         mock_bonus.return_value = 1
         s = salary.Salary(year=2017)
         assert s.calculation_salary() == 101
         mock_bonus.assert_called()
Exemplo n.º 5
0
    def test_calculation_salary_patcher(self):
        self.mock_bonus.return_value = 1

        s = salary.Salary(year=2017)

        assert s.calculation_salary() == 101
        self.mock_bonus.assert_called()
    def test_datetime_mock(self, mocker):
        from datetime import datetime
        import time

        # salary.pyのdatetime.now()をモック化

        # 1.unittestのmockを使用したパターン
        # with patch('salary.datetime') as datetime_mock:
        #     datetime_mock.now.return_value = datetime(2019, 1, 1, 10, 11, 20)
        #     datetime_mock.utcnow.return_value = datetime(2019, 1, 1, 1, 11, 20)
        #     s = salary.Salary()
        #     datetime_now = s.now_date_time()
        #     datetime_now_utc = s.now_date_time_utc()
        # assert datetime_now == "name_2019-01-01 10:11:20"
        # assert datetime_now_utc == "name_2019-01-01 01:11:20"

        # 2.pytest-mockを使用したパターン
        datetime_mock = mocker.patch('salary.datetime', mocker.Mock())
        datetime_mock.now.return_value = datetime(2019, 1, 1, 10, 11, 20)
        datetime_mock.utcnow.return_value = datetime(2019, 1, 1, 1, 11, 20)

        # time.time()をモック化
        mocker.patch('time.time', mocker.Mock(return_value=10))

        s = salary.Salary()
        datetime_now = s.now_date_time()
        datetime_now_utc = s.now_date_time_utc()
        time_now = s.now_time()
        datetime_method_cain = s.datetime_method_cahin()

        assert datetime_now == "name_2019-01-01 10:11:20"
        assert datetime_now_utc == "name_2019-01-01 01:11:20"
        assert time_now == "name_10"
        assert datetime_method_cain == "name_2019-01-01"
Exemplo n.º 7
0
    def test_calculations_salaly_with_patcher(self):
        self.mock_bonus_price.return_value = 1

        s = salary.Salary(year=2017)
        salary_price = s.calculation_salary()

        self.assertEqual(salary_price, 101)
        self.mock_bonus_price.assert_called()
    def test_patch_object_variable(self, mocker, prepare):
        """インスタンス変数をモック化"""
        s = salary.Salary()
        mocker.patch.object(s, 'base', 400)
        mocker.patch.object(s, 'calcuration_heavy_task', return_value=500)

        result = s.calcuration_bonus()
        assert result == 900
Exemplo n.º 9
0
 def test_calculation_salary(self):
     s = salary.Salary(year=2017)
     s.bonus_api.bonus_price = MagicMock(return_value=1)
     self.assertEqual(s.calculation_salary(), 101)
     s.bonus_api.bonus_price.assert_called_once()
     s.bonus_api.bonus_price.assert_called_with(year=2017)
     s.bonus_api.bonus_price.assert_called_once_with(year=2017)
     self.assertEqual(s.bonus_api.bonus_price.call_count, 1)
Exemplo n.º 10
0
    def test_calculation_salary_patch_side_effect_refused_error(self):
        self.mock_bonus.side_effect = ConnectionRefusedError

        s = salary.Salary(year=2017)
        salary_price = s.calculation_salary()

        self.assertEqual(salary_price, 100)  # ERRORがでたらbonusは0
        self.mock_bonus.assert_called()
 def test_calculation_salary(self):
     s = salary.Salary(year=2017)
     s.bonus_api.bonus_price = MagicMock(return_value=1)
     self.assertEqual(s.calculation_salary(), 101)
     s.bonus_api.bonus_price.assert_called()                 # 本当に実体が呼ばれたか
     s.bonus_api.bonus_price.assert_called_once()            # 実体が呼ばれたのは一度か
     s.bonus_api.bonus_price.assert_called_with(year=2017)   # 実体への引数は正しいか
     self.assertEqual(s.bonus_api.bonus_price.call_count, 1)
Exemplo n.º 12
0
    def test_calculation_salary_patch(self, mock_bonus):
        mock_bonus.return_value = 1

        s = salary.Salary(year=2017)
        salary_price = s.calcuration_salary()

        self.assertEqual(salary_price, 101)
        mock_bonus.assert_called()
Exemplo n.º 13
0
 def test_calculation_salary(self):
     s = salary.Salary(year=2017)
     s.bonus_api.bonus_price = MagicMock(return_value=1)
     self.assertEqual(s.calculation_salary(), 101)
     s.bonus_api.bonus_price.assert_called() #このメソッドが呼ばれたか確認する 
     s.bonus_api.bonus_price.assert_called_once() # 一回だけ読んだかを確かめる
     # s.bonus_api.bonus_price.assert_called_with(year=2018) # エラーが出る 2018を渡しているかを確認する
     # s.bonus_api.bonus_price.assert_called_once_with(year=2018) # once + withなメソッド
     self.assertEqual(s.bonus_api.bonus_price.call_count, 1)# なんかい呼ばれたかを確認する
Exemplo n.º 14
0
    def test_calculation_salary_mock_class(self, mock_rest):
        # return_valueでモック対象のクラスのインスタンスが返る
        mock_rest = mock_rest.return_value
        mock_rest.bonus_price.return_value = 1

        s = salary.Salary(year=2017)
        salary_price = s.calculation_salary()

        self.assertEqual(salary_price, 101)
    def test_calculation_salary_patch_with(self):
        with mock.patch('salary.ThirdPartyBonusApi.bonus_price') as mock_bonus:
            mock_bonus.return_value = 1

            s = salary.Salary(year=2017)
            salary_price = s.calculation_salary()

            self.assertEqual(salary_price, 101)
            mock_bonus.assert_called()
Exemplo n.º 16
0
    def test_calculation_salary_mock(self, mock_bonus):
        # 第二引数に指定した任意の変数名がモックオブジェクトとなる
        mock_bonus.return_value = 1

        s = salary.Salary(year=2017)
        salary_price = s.calculation_salary()

        self.assertEqual(salary_price, 101)
        mock_bonus.assert_called()
Exemplo n.º 17
0
    def test_calculation_salary_class(self, mock_rest):
        mock_rest = mock_rest.return_value
        # mock_rest = MockRest()
        mock_rest.bonus_price.return_value = 1

        s = salary.Salary(year=2017)
        salary_price = s.calcuration_salary()

        self.assertEqual(salary_price, 101)
        mock_rest.bonus_price.assert_called()
    def test_calculation_salary_patch_side_effect(self):
        # def f(year):
        #     return year*2
        self.mock_bonus.side_effect = lambda year: 1    # fと同義

        s = salary.Salary(year=2017)
        salary_price = s.calculation_salary()

        self.assertEqual(salary_price, 101)
        self.mock_bonus.assert_called()
Exemplo n.º 19
0
    def test_calculations_salaly_patch_with(self):
        with mock.patch("salary.ThirdPartyBonusRestAPI.bonus_price"
                        ) as mock_bonus_price:
            mock_bonus_price.return_value = 1

            s = salary.Salary(year=2017)
            salary_price = s.calculation_salary()

            self.assertEqual(salary_price, 101)
            mock_bonus_price.assert_called()
Exemplo n.º 20
0
    def test_calculation_salary_patch_with_patcher(self):
        # patcher = mock.patch('salary.ThirdPartryBonusRestAPi.bonus_price')
        # mock_bonus = self.patcher.start()
        self.mock_bonus.return_value = 1

        s = salary.Salary(year=2017)
        salary_price = s.calcuration_salary()

        self.assertEqual(salary_price, 101)
        self.mock_bonus.assert_called()
Exemplo n.º 21
0
    def test_calculation_salary_patch_with_side_effect(self):
        # def f(year):
        #     return year * 2

        self.mock_bonus.side_effect = ConnectionRefusedError

        s = salary.Salary(year=2017)
        salary_price = s.calcuration_salary()

        self.assertEqual(salary_price, 100)
        self.mock_bonus.assert_called()
Exemplo n.º 22
0
    def test_calculation_salary_patch(self, mock_rest):
        mock_rest = mock_rest.return_value
        # mock_rest = MockRest()
        mock_rest.bonus_price.return_value = 1
        mock_rest.get_api_name.return_value = 'Money'

        s = salary.Salary(year=2017)
        salary_price = s.calculation_salary()

        self.assertEqual(salary_price, 101)
        mock_rest.bonus_price.assert_called()
Exemplo n.º 23
0
    def test_calculation_salary_patch_with(self):
        # withステートメントにすればテスト内でも限られた部分のみモックを使用することができる
        with mock.patch(
                'salary.ThirdPartyBounusRestApi.bonus_price') as mock_bonus:
            mock_bonus.return_value = 1

            s = salary.Salary(year=2017)
            salary_price = s.calcuration_salary()

            self.assertEqual(salary_price, 101)
            mock_bonus.assert_called()
Exemplo n.º 24
0
    def test_calculation_salary(self):
        s = salary.Salary(year=2017)
        s.bonus_api.bonus_price = MagicMock(return_value=1)
        assert s.calculation_salary() == 101
        # 本当にこの関数は呼ばれたのか
        s.bonus_api.bonus_price.assert_called()
        # 1回だけ呼ばれたのか
        s.bonus_api.bonus_price.assert_called_once()
        assert s.bonus_api.bonus_price.call_count == 1

        s.bonus_api.bonus_price.assert_called_with(year=2017)
 def test_calculation_salary(self, mocker, prepare):
     s = salary.Salary(year=2017)
     s.bonus_api.bonus_price = mocker.patch(
         'salary.ThirdPartryBonusRestAPi.bonus_price',
         side_effect=dummy.dummy_bonus_price)
     assert s.calcuration_salary() == 101
     s.bonus_api.bonus_price.assert_called()
     s.bonus_api.bonus_price.assert_called_once()
     s.bonus_api.bonus_price.assert_called_with(year=2017)
     s.bonus_api.bonus_price.assert_called_once_with(year=2017)
     assert s.bonus_api.bonus_price.call_count == 1
Exemplo n.º 26
0
    def test_calculation_salary_mock_patcher(self):
        patcher = mock.patch('salary.ThirdPartyBonusAPI.bonus_price')
        # start()からstop()の間だけmock化する
        mock_bonus = patcher.start()
        mock_bonus.return_value = 1

        s = salary.Salary(year=2017)
        salary_price = s.calculation_salary()

        self.assertEqual(salary_price, 101)
        mock_bonus.assert_called()
        patcher.stop()
Exemplo n.º 27
0
    def test_calculations_salaly_patch_with_side_effect(self):
        # 関数の形で書き換えられる。 この部分はlambdaでもよい
        def f(year):
            return 1

        self.mock_bonus_price.side_effect = f
        self.mock_bonus_price.side_effect = ConnectionRefusedError

        s = salary.Salary(year=2017)
        salary_price = s.calculation_salary()

        self.assertEqual(salary_price, 100)
        self.mock_bonus_price.assert_called()
Exemplo n.º 28
0
    def test_calculation_salary_patch_side_effect_list(self):
        self.mock_bonus.side_effect = [1, 2, 3, ValueError('Bankrupt')]

        # 1
        s = salary.Salary(year=2017)
        salary_price = s.calculation_salary()
        self.assertEqual(salary_price, 101)

        # 2
        s = salary.Salary(year=2018)
        salary_price = s.calculation_salary()
        self.assertEqual(salary_price, 102)

        # 3
        s = salary.Salary(year=2019)
        salary_price = s.calculation_salary()
        self.assertEqual(salary_price, 103)

        # 4
        s = salary.Salary(year=200)
        with self.assertRaises(ValueError):
            salary_price = s.calculation_salary()
Exemplo n.º 29
0
    def test_calculation_salary_patch_side_effect(self):
        # def f(year):
        #     return 1

        self.mock_bonus.side_effect = [
            1,
            2,
            3,
            ValueError('Bankrupt!!!')]

        s = salary.Salary(year=2017)
        salary_price = s.calculation_salary()
        self.assertEqual(salary_price, 101)
        s = salary.Salary(year=2018)
        salary_price = s.calculation_salary()
        self.assertEqual(salary_price, 102)
        s = salary.Salary(year=2019)
        salary_price = s.calculation_salary()
        self.assertEqual(salary_price, 103)
        s = salary.Salary(year=200)
        with self.assertRaises(ValueError):
            s.calculation_salary()
Exemplo n.º 30
0
    def test_calculation_salary(self):
        s = salary.Salary(year=2017)
        s.bonus_api.bonus_price = MagicMock(return_value=1)
        self.assertEqual(s.calcuration_salary(), 101)

        # 処理内で関数がコールされたか
        s.bonus_api.bonus_price.assert_called()
        # 処理内で関数が1回だけコールされたか
        s.bonus_api.bonus_price.assert_called_once()
        # 処理内で関数が指定したパラメータでコールされたか
        s.bonus_api.bonus_price.assert_called_with(year=2017)
        # 処理内で関数が1回だけ指定したパラメータでコールされたか
        s.bonus_api.bonus_price.assert_called_once_with(year=2017)
        # 処理内で関数がコールされた回数
        self.assertEqual(s.bonus_api.bonus_price.call_count, 1)