Skip to content

Commit

Permalink
Use tox to test the python3-flask template
Browse files Browse the repository at this point in the history
**What**
- Add a sample tox.ini file to the python3-flask. This configuration
  will run pytest and flake8 for the function handler. The default
  flake8 is very resticted, only erroring for undefined names and
  syntax errors. This allows us to lint and test the function during
  `faas-cli build`, which should simplify CI flows for users. Using
  `tox` allows th euser to completely customize the test flow without
  requiring any changes to the template.
- Add same test for the echo handler
- Include documentationthat describes how to disable

Signed-off-by: Lucas Roesler <roesler.lucas@gmail.com>
  • Loading branch information
LucasRoesler authored and alexellis committed Mar 25, 2021
1 parent b7057b6 commit fef5672
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
9 changes: 5 additions & 4 deletions template/python3-flask/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ WORKDIR /home/app/function/
COPY function/requirements.txt .
RUN pip install --user -r requirements.txt

WORKDIR /home/app/

#install function code
USER root

COPY function function
RUN chown -R app:app ./
COPY function/ .
RUN chown -R app:app ../
RUN tox -l == 0 && tox

WORKDIR /home/app/

#configure WSGI server and healthcheck
USER app
Expand Down
8 changes: 8 additions & 0 deletions template/python3-flask/function/handler_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from .handler import handle

# Test your handler here
# To disable testing, you need to update the
# tox.ini file, in addition to deleting this file.

def test_handle():
assert handle("input") == "input"
41 changes: 41 additions & 0 deletions template/python3-flask/function/tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# If you would like to disable
# automated testing during faas-cli build,

# Replace the content of this file with
# [tox]
# skipsdist = true

# You can also edit, remove, or add additional test steps
# by editing, removing, or adding new testenv sections


# find out more about tox: https://tox.readthedocs.io/en/latest/
[tox]
envlist = lint,test
skipsdist = true

[testenv:test]
deps =
flask
pytest
-rrequirements.txt
commands =
# run unit tests with pytest
# https://docs.pytest.org/en/stable/
# configure by adding a pytest.ini to your handler
pytest

[testenv:lint]
deps =
flake8
commands =
flake8 .

[flake8]
count = true
max-line-length = 127
max-complexity = 10
statistics = true
# stop the build if there are Python syntax errors or undefined names
select = E9,F63,F7,F82
show-source = true
2 changes: 1 addition & 1 deletion template/python3-flask/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
flask
waitress

tox==3.*

0 comments on commit fef5672

Please sign in to comment.