Test Python non-functional programs with GitLab CI

Overview

Because there was an order to teach automatic testing at the same time in Python beginner level education We investigated whether automatic testing can be performed on programs before learning about functions.

Requirements

I haven't decided what to use for the Git server yet, so I checked the automated test procedure at GitLab, which I recently started using.

Program to be tested

chapter_01_1.py


height = input('height(m) ')
width = input('Bottom(m) ')
area = float(height) * float(width) / 2
print('The area is'+str(area)+'Square meter')

Test specification file

Since it is a program that accepts standard input, it uses shell redirection to populate the contents of the test input file.

tests/test_chapter01.py


def test_chapter01_1(bash):
    assert bash.run_script_inline(['python3 chapter_01_1.py < tests/stdin_chapter_01_1.txt']) == 'height(m)Bottom(m)Area is 1.2 square meters'

Test input file

A line break is required after the input value.

tests/stdin_chapter_01_1.txt


1.2
2.0

Run local test

Although it is written as bash in the test script, it naturally works with zsh.

% pytest
============================================================ test session starts ============================================================
platform darwin -- Python 3.7.3, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: /Users/takaiayumu/python-basic
plugins: openfiles-0.3.2, arraydiff-0.3, shell-0.2.3, doctestplus-0.3.0, remotedata-0.3.1, cov-2.8.1
collected 1 items

tests/test_chapter01.py ..                                                                                                             [100%]

========================================================= 1 passed in 1.45 seconds ==========================================================

Settings for GitLab CI

I use the image of Python3, but pytest-shell does not work on the base Alpine Linux because bash is not installed by default. Therefore, you need to install bash with the apk command.

.gitlab-ci.yml


image: python:3-alpine

before_script:
    - apk update
    - apk add bash
    - pip install pytest pytest-cov pytest-shell

test:
    script:
        - pytest --cov=./tests

Recommended Posts

Test Python non-functional programs with GitLab CI
Primality test with Python
Primality test with python
Static analysis of Python code with GitLab CI
Unit test log output with python
[Python] Automate Pelican builds with Travis CI
[Python] Super easy test with assert statement
Stress Test with Locust written in Python
WebUI test with Python2.6 + Selenium 2.44.0 --profile setting
Generate Japanese test data with Python faker
Post Test 3 (Working with PosgreSQL in Python)
How to do portmanteau test with python
Integrating with setuptools / python setup.py test / pytest-runner
FizzBuzz with Python3
Scraping with Python
Create test data like that with Python (Part 1)
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Python Integrity Test
Bingo with python
Zundokokiyoshi with python
[Small story] Test image generation with Python / OpenCV
ARC037 Baum test politely with Python recursive function
Track Python programs
Excel with Python
Microcomputer with Python
Cast with python
1st Algorithm Practical Test Solve past questions with python
[Automation] Operate GitLab with Python to facilitate inquiry management
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Strengthen with code test ⑦
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Strengthen with code test ⑨
Learning Python with ChemTHEATER 05-1
Strengthen with code test ③
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python