Python development helped by Jenkins-Unit test

Summary of this article

I will explain how to get Jenkins to do unit tests in development using Python. Nose or py.test are good candidates for Python's unit testing framework. This time, we will use py.test.

To Jenkins

The environment is as follows.

item Version etc.
Python 3.5.1
Jenkins 1.6...?
Test framework py.test

py.test Install with pip install pytest

Test code example

Code to be tested --calculator.py

class Calculator():
    def add(self, a, b):
        return a + b

    def sub(self, a, b):
        return a - b

    def mul(self, a, b):
        return a * b

    def div(self, a, b):
        return a / b

Test code --test_calculator.py

import pytest
from calculator import Calculator

def pytest_funcarg__calc():
    return Calculator()

@pytest.mark.parametrize("a, b, r", [(9, 8, 17), (7, 6, 13), (5, 4, 9), (3, 2, 5), (1, 0, 1)])
def test_add(calc, a, b, r):
    assert calc.add(a, b) == r

@pytest.mark.parametrize("a, b, r", [(9, 8, 1), (7, 6, 1), (5, 4, 1), (3, 2, 1), (1, 0, 1)])
def test_sub(calc, a, b, r):
    assert calc.sub(a, b) == r

@pytest.mark.parametrize("a, b, r", [(9, 8, 72), (7, 6, 42), (5, 4, 20), (3, 2, 6), (1, 0, 0)])
def test_mul(calc, a, b, r):
    assert calc.mul(a, b) == r

@pytest.mark.parametrize("a, b, r", [(9, 3, 3), (8, 4, 2), (6, 2, 3), (4, 2, 2), (0, 1, 0)])
def test_div(calc, a, b, r):
    assert calc.div(a, b) == r

def test_div_error(calc):
    with pytest.raises(ZeroDivisionError):
        calc.div(1, 0)

Test run

Can be executed with py.test Recursively search under the executed directory and It runs py files that start with test.

test results Success story

============================= test session starts ==============================
platform darwin -- Python 3.5.1, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: /Users/rotasuke/git/python_test/others, inifile: 
collected 21 items

test_calculator.py .....................

========================== 21 passed in 0.03 seconds ===========================

Failure example

============================= test session starts ==============================
platform darwin -- Python 3.5.1, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: /Users/rotasuke/git/python_test/others, inifile: 
collected 21 items

test_calculator.py ...................F.

=================================== FAILURES ===================================
_______________________________ test_div[0-1-1] ________________________________

calc = <calculator.Calculator object at 0x10c9567f0>, a = 0, b = 1, r = 1

    @pytest.mark.parametrize("a, b, r", [(9, 3, 3), (8, 4, 2), (6, 2, 3), (4, 2, 2), (0, 1, 1)])
    def test_div(calc, a, b, r):
>       assert calc.div(a, b) == r
E       assert 0.0 == 1
E        +  where 0.0 = <bound method Calculator.div of <calculator.Calculator object at 0x10c9567f0>>(0, 1)
E        +    where <bound method Calculator.div of <calculator.Calculator object at 0x10c9567f0>> = <calculator.Calculator object at 0x10c9567f0>.div

test_calculator.py:21: AssertionError
===================== 1 failed, 20 passed in 0.04 seconds ======================

Jenkins job creation

Detailed settings such as Git checkout are omitted.

In [Build]-[Run Shell] py.test <directory to run> To specify.

Test report output

In the shell script to be executed that you entered earlier --junitxml = <destination xml file> By specifying, you can output the test report in JUnit format.

Added [Aggregate JUnit test results] to [Post-build processing] Specify the xml file that was output earlier.

Coverage output

Install the module for coverage output with pip install pytest-cov

In the shell script to be executed that you entered earlier --cov-report=xml --cov By specifying, coverage.xml can be output under the directory where the command is executed.

It can be visualized by having Jenkins' Cobertura Plugin read coverage.xml.

Recommended Posts

Python development helped by Jenkins-Unit test
Primality test by Python
Python Integrity Test
Primality test with Python
Visualization memo by Python
Communication processing by Python
Python package development notes
Python basics 8 numpy test
Python test package memo
Python development environment construction
Primality test with python
About Python development environment
python2.7 development environment construction
Beamformer response by python
Framework development in Python
Development environment in Python
python tag integration test
python unit test template
Bluemix Python Microservices Development
Slackbot development in Python
Algorithm in Python (primality test)
EXE Web API by Python
Newcomer training program by Python
Pin python managed by conda
Organize your Python development environment
Keyword extraction by MeCab (python)
Separate numbers by 3 digits (python)
[ev3dev × Python] Build ev3dev development environment
Python template for Codeforces-manual test-
Markov switching model by Python
Python development in Visual Studio 2017
Image processing by python (Pillow)
Python started by C programmers
Python debug and test module
[MEMO] [Development environment construction] Python
[For organizing] Python development environment
Python development in Visual Studio
Platform (OS) determination by Python
Sort by date in python
Set python test in jenkins
A memorandum of scraping & machine learning [development technique] by Python (Chapter 4)
A memorandum of scraping & machine learning [development technique] by Python (Chapter 5)
[Pytest] [mock] Web development beginners summarized unit test and mock in python.