adding tests and fix the pip install
This commit is contained in:
@@ -9,12 +9,14 @@ build job:
|
||||
- apt-get update -qy
|
||||
- apt-get install -y python3-dev python3-pip python3.12-venv
|
||||
- python3 -V
|
||||
- pip install pdm
|
||||
- pdm install
|
||||
- pip3 install -r requirements.txt
|
||||
|
||||
# launch tests
|
||||
- export PYTHONPATH=.
|
||||
- export FLASK_APP=app
|
||||
- pdm run pytest --cov --cov-report term --cov-report html
|
||||
- pytest tests --cov --cov-report term --cov-report html
|
||||
|
||||
|
||||
artifacts:
|
||||
paths:
|
||||
- src/htmlcov/
|
||||
|
4
src/requirements.txt
Normal file
4
src/requirements.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
Flask
|
||||
flask-wtf
|
||||
pytest
|
||||
pytest-cov
|
0
src/tests/__init__.py
Normal file
0
src/tests/__init__.py
Normal file
12
src/tests/conftest.py
Normal file
12
src/tests/conftest.py
Normal file
@@ -0,0 +1,12 @@
|
||||
import pytest
|
||||
from app import app
|
||||
|
||||
# see documentation under https://flask.palletsprojects.com/en/2.0.x/testing/
|
||||
|
||||
|
||||
# the client here allow to use the app without running in live server
|
||||
# see https://flask.palletsprojects.com/en/2.0.x/testing/#sending-requests-with-the-test-client
|
||||
@pytest.fixture
|
||||
def client():
|
||||
app.config['TESTING'] = True
|
||||
yield app.test_client()
|
65
src/tests/test_api.py
Normal file
65
src/tests/test_api.py
Normal file
@@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2023, School of Engineering and Architecture of Fribourg
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from urllib.parse import urlencode
|
||||
import json
|
||||
|
||||
__author__ = 'Michael Mader'
|
||||
__date__ = "2023-03-12"
|
||||
__version__ = "0.2"
|
||||
__email__ = "michael.maeder@hefr.ch"
|
||||
|
||||
|
||||
def call(client, path, params):
|
||||
"""calling function that simulates an API webcall of a specific function/route
|
||||
|
||||
Args:
|
||||
client: this is the client object used by pytest to 'simulate' the API without running the webserver
|
||||
path: route of the API to use
|
||||
params: GET parameter that are passed to the function
|
||||
|
||||
Returns:
|
||||
json: the result of the client call
|
||||
"""
|
||||
url = path + '?' + urlencode(params)
|
||||
response = client.get(url)
|
||||
return json.loads(response.data.decode('utf-8'))
|
||||
|
||||
# increment test 1
|
||||
def test_plus_one1(client):
|
||||
result = call(client, '/inc', {'x': 2})
|
||||
assert result['x'] == 3
|
||||
|
||||
# increment test 1
|
||||
def test_plus_one2(client):
|
||||
result = call(client, '/inc', {'x': -2})
|
||||
assert result['x'] == -1
|
||||
|
||||
# adding test with negative value
|
||||
def test_plus_y(client):
|
||||
result = call(client, '/add', {'x': -2, 'y': 7})
|
||||
assert result['result'] == 5
|
||||
|
||||
# multiplication test
|
||||
def test_multiply(client):
|
||||
result = call(client, '/mul', {'x': -2, 'y': 7})
|
||||
assert result['result'] == -14
|
||||
|
||||
# division test
|
||||
def test_division(client):
|
||||
result = call(client, '/div', {'x': 35, 'y': 7})
|
||||
assert result['result'] == 5
|
Reference in New Issue
Block a user