From 7f8250c0a6a6f23e17729c4442030be4f4d553fe Mon Sep 17 00:00:00 2001 From: scito Date: Sat, 3 Sep 2022 15:38:47 +0200 Subject: [PATCH] add github test action and add pytest --- .github/workflows/extract_otp_secret_keys.yml | 35 +++++++++ README.md | 19 ++++- test_extract_otp_secret_keys_pytest.py | 77 +++++++++++++++++++ ...> test_extract_otp_secret_keys_unittest.py | 0 4 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/extract_otp_secret_keys.yml create mode 100644 test_extract_otp_secret_keys_pytest.py rename unittest_extract_otp_secret_keys.py => test_extract_otp_secret_keys_unittest.py (100%) diff --git a/.github/workflows/extract_otp_secret_keys.yml b/.github/workflows/extract_otp_secret_keys.yml new file mode 100644 index 0000000..d0e5789 --- /dev/null +++ b/.github/workflows/extract_otp_secret_keys.yml @@ -0,0 +1,35 @@ +name: extract_otp_secret_keys + +on: [push] + +jobs: + build: + + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.7", "3.8", "3.9", "3.10"] + + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install flake8 pytest + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: Lint with flake8 + run: | + # stop the build if there are Python syntax errors or undefined names + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + - name: Test with pytest + run: | + pytest + - name: Test with unittest + run: | + python -m unittest diff --git a/README.md b/README.md index 0186428..a50030d 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,9 @@ Install [devbox](https://github.com/jetpack-io/devbox), which is a wrapper for n devbox shell ``` -## Unit Tests +## Tests + +### unittest There are basic unit tests, see `unittest_extract_otp_secret_keys.py`. @@ -72,3 +74,18 @@ Run unit tests: ``` python -m unittest ``` + +### PyTest + +There are basic pytests, see `test_extract_otp_secret_keys.py`. + +Run pytests: + +``` +pytest unittest +``` +or + +``` +python -m pytest +``` diff --git a/test_extract_otp_secret_keys_pytest.py b/test_extract_otp_secret_keys_pytest.py new file mode 100644 index 0000000..8aa6de4 --- /dev/null +++ b/test_extract_otp_secret_keys_pytest.py @@ -0,0 +1,77 @@ +# pytest for extract_otp_secret_keys.py + +# Run tests: +# pytest + +# Author: Scito (https://scito.ch) + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import csv +import json +import os + +import extract_otp_secret_keys + +def test_extract_csv(): + # Arrange + cleanup() + + # Act + extract_otp_secret_keys.main(['-q', '-c', 'test_example_output.csv', 'example_export.txt']) + + # Assert + expected_csv = read_csv('example_output.csv') + actual_csv = read_csv('test_example_output.csv') + + assert actual_csv == actual_csv + + # Clean up + cleanup() + +def test_extract_json(): + # Arrange + cleanup() + + # Act + extract_otp_secret_keys.main(['-q', '-j', 'test_example_output.json', 'example_export.txt']) + + expected_json = read_json('example_output.json') + actual_json = read_json('test_example_output.json') + + assert actual_json == expected_json + + # Clean up + cleanup() + +def cleanup(): + remove_file('test_example_output.csv') + remove_file('test_example_output.json') + +def remove_file(filename): + if os.path.exists(filename): os.remove(filename) + +def read_csv(filename): + """Returns a list of lines.""" + with open(filename, "r") as infile: + lines = [] + reader = csv.reader(infile) + for line in reader: + lines.append(line) + return lines + +def read_json(filename): + """Returns a list or a dictionary.""" + with open(filename, "r") as infile: + return json.load(infile) diff --git a/unittest_extract_otp_secret_keys.py b/test_extract_otp_secret_keys_unittest.py similarity index 100% rename from unittest_extract_otp_secret_keys.py rename to test_extract_otp_secret_keys_unittest.py