LoginSignup
1
3

More than 3 years have passed since last update.

【pytest】【mock】Web開発初心者がpythonでのunit testとmockについてまとめてみた。

Posted at

はじめに

 Web開発初心者が、pytestによるunit testと、Django mock queriesによるmockを学んだので、アウトプットしていきます。

unit testとは?mockとは?

 unit testとは、プログラムの比較的小さな構成単位(通常は関数やメソッド)が正しく機能しているかを検証するテストのことである。
 mockとは、プログラムのテストに必要な構成部分の値を擬似的に設定するものである。

unit testの嬉しさ

 読んで字のごとく、小さな構成単位での検証が行えるので、ミスが特定しやすい。また、テストケースをメソッドとして残しておけるため、プログラムに変更があっても、同じテストを再現できる。

mockの嬉しさ

 あるクラスXのテストに必要なクラスYが未完成でも、mockを使うことでクラスXのテストが可能になる。要は、unit testの効率を上げることができる。

とりあえずpytestを使ってみる

 まず、unit testを行う対象のプログラムは、AtCoder Begginers Contest 141 E問題のWho Says a Pun?の解答を少し変えたものにします。

qiita.py
def length(n, s):
    res = 0
    i,j = 0, 1
    while j < n:
        if s[i:j] in s[j:]:
            res = max(res, j-i)
            j += 1
        else:
            i += 1
    if i == j:
        i += 1
        j += 2
    return res

次に、unit test用のプログラムを用意します。

test_qiita.py
from qiita import length

def test_1_ac():
    assert length(5, 'ababa') == 2

def test_1_wa():
    assert length(5, 'ababa') == 0

def test_2_ac():
    assert length(2, 'xy') == 0

def test_2_wa():
    assert length(2, 'xy') == 7

def test_3_ac():
    assert length(13, 'strangeorange') == 5

def test_3_wa():
    assert length(13, 'strangeorange') == 6

pytestを実行します。

$pytest test_qiita.py

実行結果:

======================================= test session starts =======================================
platform win32 -- Python 3.7.4, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: C:/Users/~/qiita
plugins: arraydiff-0.3, cov-2.8.1, doctestplus-0.4.0, openfiles-0.4.0, remotedata-0.3.2
collected 6 items

test_qiita.py .F.F.F                                                                         [100%]

============================================ FAILURES =============================================
____________________________________________ test_1_wa ____________________________________________ 

    def test_1_wa():
>       assert length(5, 'ababa') == 0
E       AssertionError: assert 2 == 0
E        +  where 2 = length(5, 'ababa')

test_qiita.py:7: AssertionError
____________________________________________ test_2_wa ____________________________________________ 

    def test_2_wa():
>       assert length(2, 'xy') == 7
E       AssertionError: assert 0 == 7
E        +  where 0 = length(2, 'xy')

test_qiita.py:13: AssertionError
____________________________________________ test_3_wa ____________________________________________ 

    def test_3_wa():
>       assert length(13, 'strangeorange') == 6
E       AssertionError: assert 5 == 6
E        +  where 5 = length(13, 'strangeorange')

test_qiita.py:19: AssertionError
=================================== 3 failed, 3 passed in 0.11s =================================== 

ちゃんと間違ってる答えを見つけてくれました。

まとめ

 特に大きなプログラムの中で、部分的な処理(関数)が想定通りの答えを返してくれているかを確認するのに非常に便利であることが分かった。開発を行う際の動作確認・デバッグに盛んに使っていきたい。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3