Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bootstrap Unit Test framework #412

Merged
merged 5 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,32 @@ jobs:
run: |
echo "::add-matcher::.github/matchers/pylint.json"
tox -e pylint

# Run python unit tests
# https://docs.python.org/3/library/unittest.html
tests-unit:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Python 3.8
uses: actions/setup-python@v4
with:
python-version: "3.8"
cache: 'pip'

- name: Cache Python Dependencies and Env
uses: actions/cache@v2
with:
path: |
~/.cache/pip
.tox
key: ${{ runner.os }}-v1-python-3.8-pylint-${{ hashFiles('requirements.txt', 'test-requirements.txt', 'tox.ini') }}

- name: Install Python test dependencies
run: python -m pip install tox

- name: Run unit tests
run: |
tox -e unit
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
*.logs

# VS code
.vscode/settings.json
.vscode/
# We include in the repo recommended settings file with configured Tests Suite
!.vscode/settings.json

# Mac
**/.DS_Store
Expand Down
17 changes: 17 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Configuration for running unit tests with the Python extension for VS Code
// https://code.visualstudio.com/docs/python/unit-testing
{
"python.testing.unittestArgs": [
"--verbose",
"--top-level-directory",
"scripts",
"--start-directory",
"./scripts/tests/unit",
"--pattern",
"test_*.py"
],
"python.testing.pytestEnabled": false,
// Uses the unittest module
// https://docs.python.org/3/library/unittest.html
"python.testing.unittestEnabled": true
}
Copy link
Member Author

@arm4b arm4b Mar 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've also included the VSCode project-level settings.json.
Should help to get the unit-tests auto-discovered here with zero hassle:
image

17 changes: 17 additions & 0 deletions scripts/tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# BitOps Unit Tests

This directory contains tests for the BitOps core.
We use the built-in [`unittest`](https://docs.python.org/3/library/unittest.html) module as a framework for testing.

## Running the tests
The testing command is defined in [`tox.ini`](../../tox.ini) and can be run with `tox`:
```bash
tox -e unit
```

## VSCode Configuration
The repository contains a `.vscode/settings.json` file that configures VSCode to auto-discover the tests and run the test suite.

![VSCode Tests Suite Configuration](https://code.visualstudio.com/assets/docs/python/testing/test-results.png)

See [Python Testing in VSCode](https://code.visualstudio.com/docs/python/testing) for more information.
Empty file added scripts/tests/__init__.py
Empty file.
Empty file added scripts/tests/unit/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions scripts/tests/unit/test_hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import unittest


class TestHello(unittest.TestCase):
"""Basic test example demonstrating the hello world."""

def test_hello(self):
"""Test hello world."""
self.assertEqual(True, True)


if __name__ == "__main__":
unittest.main()
8 changes: 8 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,11 @@ commands =
skip_install = True
commands =
pylint scripts/

# Unit tests
# https://docs.python.org/3/library/unittest.html
# Run:
# tox -e unit
[testenv:unit]
commands =
python3 -m unittest discover --verbose --top-level-directory scripts --start-directory scripts/tests/unit --pattern "test_*.py"
Comment on lines +36 to +43
Copy link
Member Author

@arm4b arm4b Mar 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out, this part was the hardest that took us a lot of time debugging to make the imports work with the directory structure and tests residing in the scripts/plugins/unit/tests/ directory.

Notice the --top-level-directory scripts and --start-directory scripts/tests/unit parameters.