[Pytest] [mock] Web development beginners summarized unit test and mock in python.

Introduction

Web development beginners have learned unit tests with pytest and mock with Django mock queries, so we will output them.

What is a unit test? What is a mock?

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.

The joy of unit test

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.

The joy of mock

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 **.

Try using pytest for the time being

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.

Summary

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

[Pytest] [mock] Web development beginners summarized unit test and mock in python.
Web application development memo in python
Development and deployment of REST API in Python using Falcon Web Framework
Prime number enumeration and primality test in Python
Rock-paper-scissors poi in Python for beginners (answers and explanations)
Directory structure for test-driven development using pytest in python
Write code to Unit Test a Python web app
Framework development in Python
python unit test template
Daemonize and persist Python web apps in Systemd-Ubuntu 18.04.3 LTS
Slackbot development in Python
[Ipdb] Web development beginners tried to summarize debugging with Python
I tried programming the chi-square test in Python and Java.
[Tips] Problems and solutions in the development of python + kivy
Specification generation and code generation in REST API development (Python edition)
Web scraping notes in python3
Algorithm in Python (primality test)
Unit test flask with pytest
Stack and Queue in Python
Python development in Visual Studio 2017
Python debug and test module
Unittest and CI in Python
Python development in Visual Studio
Set python test in jenkins
A story about everything from data collection to AI development and Web application release in Python (3. AI development)
MIDI packages in Python midi and pretty_midi
python web scraping-get elements in bulk
Difference between list () and [] in Python
Difference between == and is in python
View photos in Python and html
Automatic minimum testing in web development
Sorting algorithm and implementation in Python
Manipulate files and folders in Python
About dtypes in Python and Cython
Python development helped by Jenkins-Unit test
Assignments and changes in Python objects
[Python] Test sample using unittest2, mock
Check and move directories in Python
Unit test log output with python
Write selenium test code in python
Hashing data in R and Python
Hit the web API in Python
Function synthesis and application in Python
Statistical test (multiple test) in Python: scikit_posthocs
Export and output files in Python
Beginners use Python for web scraping (1)
Run unittests in Python (for beginners)
Reverse Hiragana and Katakana in Python2.7
Reading and writing text in Python
[GUI in Python] PyQt5-Menu and Toolbar-
web coder tried excel in Python
Beginners use Python for web scraping (4) ―― 1
Write C unit tests in Python
Create and read messagepacks in Python
Beginners use Python for web scraping (4) -3 GCE VM instance creation and scraping on VM
Experience Part I "Multinational Currencies" in the book "Test Driven Development" in Python
Let's throw away JavaScript and write a web front end in Python!