目次
- テストコード作成
- テスト実行
テストコード作成
今回は簡単な計算のクラスを作成して計算するメソッドを埋め込みます。
テストケースは以下を考えて作成してみました。
- テストが正常に終了
- テストの際に計算のメソッドを間違えて使用
あんまりテストとして成り立ってないけど。。。 - メソッドに問題があってエラーが出てしまう
test_sample.py
import pytest
class Calculate():
#コンストラクタ
def __init__(self, number1, number2):
self.number1 = number1
self.number2 = number2
#足し算
def addition(self):
return self.number1 + self.number2
#引き算
def substraction(self):
return self.number1 - self.number2
#掛け算
def multiplication(self):
return self.number1 * self.number2
#割り算
def division(self):
return self.number1 / self.number2
class Test:
#全部テストOK
def test_001(self):
Calculate001 = Calculate(10, 2)
assert Calculate001.addition() == 12
assert Calculate001.substraction() == 8
assert Calculate001.multiplication() == 20
assert Calculate001.division() == 5
#掛け算の答えが違うからエラーになるよ
def test_002(self):
Calculate002 = Calculate(6, 3)
assert Calculate002.addition() == 9
assert Calculate002.substraction() == 3
assert Calculate002.multiplication() == 10
assert Calculate002.division() == 2
#割り算でエラーになるよ
def test_003(self):
Calculate003 = Calculate(20, 0)
assert Calculate003.addition() == 20
assert Calculate003.substraction() == 20
assert Calculate003.multiplication() == 0
assert Calculate003.division() == 0
テスト実行
以下が実行結果です。
bash
root@c74f21a6e36a:/pytest_sample# pytest
================================== test session starts ===================================
platform linux -- Python 3.7.2, pytest-4.2.1, py-1.7.0, pluggy-0.8.1
rootdir: /pytest_sample, inifile:
collected 3 items
test/test_sample.py .FF [100%]
======================================== FAILURES ========================================
_____________________________________ Test.test_002 ______________________________________
self = <test_sample.Test object at 0x7fbb9dce4710>
def test_002(self):
Calculate002 = Calculate(6, 3)
assert Calculate002.addition() == 9
assert Calculate002.substraction() == 3
> assert Calculate002.multiplication() == 10
E assert 18 == 10
E + where 18 = <bound method Calculate.multiplication of <test_sample.Calculate object at 0x7fbb9dce47f0>>()
E + where <bound method Calculate.multiplication of <test_sample.Calculate object at 0x7fbb9dce47f0>> = <test_sample.Calculate object at 0x7fbb9dce47f0>.multiplication
test/test_sample.py:41: AssertionError
_____________________________________ Test.test_003 ______________________________________
self = <test_sample.Test object at 0x7fbb9e060898>
def test_003(self):
Calculate003 = Calculate(20, 0)
assert Calculate003.addition() == 20
assert Calculate003.substraction() == 20
assert Calculate003.multiplication() == 0
> assert Calculate003.division() == 0
test/test_sample.py:50:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <test_sample.Calculate object at 0x7fbb9e060ac8>
def division(self):
> return self.number1 / self.number2
E ZeroDivisionError: division by zero
test/test_sample.py:24: ZeroDivisionError
=========================== 2 failed, 1 passed in 0.06 seconds ===========================
root@c74f21a6e36a:/pytest_sample#
実行結果を見てみると以下が分かると思います。
- どこでテストが失敗したのか?
- どんなエラーなのか?