tests: Setup testing tools with pytest

Description

Abstract

Use pytest (with some sugar) for tests.

Motivation

Code is not known to be working if it’s not tested. So tests are mandatory.

Rationale

pytest is better suited to not django-only projects, which is the case here. Also with pytest we avoid inheritance hell in tests: a test must be fully understandable while reading at its code.

We also install: - pytest-cov: to enable test coverage - pytest-sugar: for a “beautiful” output of tests.

A new make command is added to run the tests:

make tests

To run a specific tests:

pytest python.path.to.tests::function-to-test

This commit includes a test_testing.py file to ensure the CI catches the tests.

Info

Hash

2342faaf808c9d6c1baf00f4558e9bde62bfee3c

Date

2019-05-27 17:14:11 +0200

Parents
  • docs: Documentation generation [91e6919e]2019-05-27 17:14:11 +0200

Children
  • style: Add flake8 and pylint to ensure code quality [4458a00c]2019-05-27 17:14:11 +0200

Branches
Tags

(No tags)

Changes

Makefile

Type

Modified

Stats

+9 -3

@@ -20,12 +20,12 @@ install:  ## Install the project in the current environment, with its dependenci
 .PHONY: dev
 dev:  ## Install the project in the current environment, with its dependencies, including the ones needed in a development environment
     @echo "$(BOLD)Installing $(PROJECT_NAME) $(PROJECT_VERSION) in dev mode$(RESET)"
-    @pip install -e .[dev,docs]
+    @pip install -e .[dev,tests,docs]
     @$(MAKE) full-clean

 .PHONY: dev-upgrade
 dev-upgrade:  ## Upgrade all default+dev dependencies defined in setup.cfg
-    @pip install --upgrade `python -c 'import setuptools; o = setuptools.config.read_configuration("setup.cfg")["options"]; print(" ".join(o["install_requires"] + o["extras_require"]["dev"] + o["extras_require"]["docs"]))'`
+    @pip install --upgrade `python -c 'import setuptools; o = setuptools.config.read_configuration("setup.cfg")["options"]; print(" ".join(o["install_requires"] + o["extras_require"]["dev"] + o["extras_require"]["tests"] + o["extras_require"]["docs"]))'`
     @pip install -e .
     @$(MAKE) full-clean

@@ -43,7 +43,7 @@ clean:  ## Clean python build related directories and files
 full-clean:  ## Like "clean" but with clean-doc and will clean some other generated directories or files
 full-clean: clean
     @echo "$(BOLD)Full cleaning$(RESET)"
-    find ./ -type d -name '__pycache__' -print0 | xargs -tr0 rm -r
+    find ./ -type d \( -name '__pycache__' -or -name '.pytest_cache'  \) -print0 | xargs -tr0 rm -r
     -$(MAKE) clean-doc

 .PHONY: doc docs
@@ -58,3 +58,9 @@ clean-doc:  ## Clean the documentation directories
     @echo "$(BOLD)Cleaning documentation directories$(RESET)"
     @rm -rf docs/source
     @cd docs && $(MAKE) clean
+
+.PHONY: tests test
+test: tests  # we allow "test" and "tests"
+tests:  ## Run tests for the isshub project.
+    @echo "$(BOLD)Running tests$(RESET)"
+    @pytest

README.rst

Type

Modified

Stats

+6 -0

@@ -145,6 +145,12 @@ The documentation will be available at `<https://isshub.readthedocs.io>`_ and wi
 Testing
 =======

+For tests I won't use unittest (and so not the Django test subclasses), but `pytest <https://docs.pytest.org/>`_
+
+And I want to do things a little bit differently that we are used too.
+
+I *may* use TDD (`Test-Driven Development <https://en.wikipedia.org/wiki/Test-driven_development>`_), but it's not sure yet, as I'm really not used to it. Will see.
+
 Git
 ===

setup.cfg

Type

Modified

Stats

+24 -0

@@ -24,12 +24,36 @@ requires-python = >=3.7
 [options]
 zip_safe = True
 packages = find:
+
+[options.packages.find]
+exclude =
+    tests
 [options.extras_require]
 dev =
     ipython
     wheel
+tests =
+    pytest
+    pytest-cov
+    pytest-sugar
 docs =
     sphinx
     sphinx-autodoc-typehints
     sphinx_rtd_theme
     sphinxprettysearchresults
+
+[tool:pytest]
+addopts =
+    --cov=isshub
+    --cov-report term-missing:skip-covered
+    --doctest-modules
+    --ignore setup.py
+    --ignore docs
+    --pyargs
+
+[coverage:run]
+branch = True
+
+[coverage:report]
+exclude_lines =
+    raise NotImplementedError

test_testing.py

Type

Added

Stats

+7 -0

@@ -0,0 +1,7 @@
+"""Test file to check testing tools"""
+
+
+def test_passing():
+    """A test that must pass"""
+
+    assert 1 == 1