Add pytest

This commit is contained in:
Freezed 2022-10-27 01:44:41 +02:00
parent b28a33345a
commit 37473f2d2f
3 changed files with 127 additions and 1 deletions

View File

@ -26,7 +26,7 @@ lint: # Lint code
open_all: # Open all projects files
${EDITOR} ${VBIN}/activate
${EDITOR} .gitlab-ci.yml main.py Makefile pyproject.toml README.md requirements-dev.txt requirements.txt
${EDITOR} .gitlab-ci.yml main.py Makefile pyproject.toml README.md requirements-dev.txt requirements.txt tests_*
${EDITOR} .git/hooks/p*-commit
pre_commit: # Run the pre-commit hook

View File

@ -2,4 +2,5 @@ black
pydocstyle
pylint
pyproject-flake8
pytest
toml

125
tests_main.py Normal file
View File

@ -0,0 +1,125 @@
#!/usr/bin/env python3
# coding: utf8
"""tests_<SCRIPT_NAME>.
Testing `main.py`
Author: {developer} <{mail}> {date}
Licence: GNU AGPL v3: http://www.gnu.org/licenses/
This file is part of [`<GIT REPO NAME>`](<GIT REPO URL>)
"""
import logging
import shlex
from pytest import mark
import main
# Faking options from argparse
class FakeOptions: # pylint: disable=too-few-public-methods
"""Options object mock."""
debug = False
foo = None # pylint: disable=disallowed-name
bar = False # pylint: disable=disallowed-name
def __init__(self, options):
"""Built loggers options."""
if "debug" in options:
self.debug = True
if "foo" in options:
self.foo = True # pylint: disable=disallowed-name
if "bar" in options:
self.bar = "BAZ" # pylint: disable=disallowed-name
def mock_parse_args_bar():
"""Mock for parse_args()."""
return FakeOptions("bar")
def mock_parse_args_foo():
"""Mock for parse_args()."""
return FakeOptions("foo")
# Faking logging
class FakeLogging: # pylint: disable=too-few-public-methods
"""Options object mock."""
def mock_setup_logging(useless_parameter): # pylint: disable=unused-argument
"""Mock for setup_logging()."""
return FakeLogging()
# ###
# Testing setup_logging()
# ###
def test_logging_debug():
"""Logger level is DEBUG. Set up with --debug argument."""
options = FakeOptions(["debug"])
main.setup_logging(options)
assert isinstance((main.logger), logging.Logger)
assert main.logger.level == 10
def test_logging_default():
"""Logger level is INFO. Set up without arguments."""
options = FakeOptions([])
main.setup_logging(options)
assert isinstance((main.logger), logging.Logger)
assert main.logger.level == 20
# ###
# Testing parse_args()
# ###
@mark.parametrize(
"args, expected",
[
("-df", {"debug": True, "foo_var": True, "bar_var": None}),
("--debug --foo", {"debug": True, "foo_var": True, "bar_var": None}),
("-db", {"debug": True, "foo_var": False, "bar_var": "BAR"}),
(
"-db BAZ",
{
"debug": True,
"foo_var": False,
"bar_var": "BAZ",
},
),
],
)
def test_parse_args(args, expected):
"""Mapping argument is set."""
cli_options = main.parse_args(shlex.split(args))
assert cli_options.debug == expected["debug"]
assert cli_options.foo == expected["foo_var"]
assert cli_options.bar == expected["bar_var"]
# ###
# Testing main()
# ###
def test_main_demo_with_foo(monkeypatch, caplog):
"""Called with options."""
monkeypatch.setattr("main.parse_args", mock_parse_args_foo)
monkeypatch.setattr("main.setup_logging", mock_setup_logging)
main.main()
assert caplog.text == "INFO pytest:main.py:155 I am foo\n"
def test_main_demo_with_bar(monkeypatch, caplog):
"""Called with options."""
monkeypatch.setattr("main.parse_args", mock_parse_args_bar)
monkeypatch.setattr("main.setup_logging", mock_setup_logging)
main.main()
assert caplog.text == "INFO pytest:main.py:158 I am BAZ\n"