Web development beginners have learned unit tests with pytest
and mock with Django mock queries
, so we will output them.
A ** unit test ** is a test that verifies that a relatively small building block (usually a function or method) is working properly. ** mock ** is to set the value of the component required for testing the program in a pseudo manner.
It is easy to identify mistakes because it can be verified in small structural units as if it were read. Also, since test cases can be left as methods, the same test can be ** reproduced ** even if the program changes.
Even if the class Y required for testing a certain class X is incomplete, mock can be used to test the class X. In short, you can increase the efficiency of ** unit test **.
First, the program to be unit tested is a slightly modified version of the AtCoder Begginers Contest 141 E question Who Says a Pun?. I will.
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
Next, prepare a program for 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
Run pytest.
$pytest test_qiita.py
Execution result:
======================================= 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 ===================================
He found the wrong answer.
It turns out to be very useful to see if a partial process (function) is returning the expected answer, especially in a large program. I want to actively use it for operation check and debugging during development.
Recommended Posts