From 07cd9bea862db45ca6998aefc1c184e8da39b15d Mon Sep 17 00:00:00 2001 From: ShivanS93 Date: Sun, 19 Sep 2021 11:13:03 +1200 Subject: [PATCH] pytest material --- .gitignore | 142 ++++++++++++++++++++++++++++ .pre-commit-config.yaml | 25 +++++ README.MD | 0 intro_pytest/Pipfile | 1 + intro_pytest/Pipfile.lock | 68 ++++++++++++- intro_pytest/function.py | 22 +++++ intro_pytest/tests/__init__.py | 0 intro_pytest/tests/test_function.py | 30 ++++++ 8 files changed, 286 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 .pre-commit-config.yaml create mode 100644 README.MD create mode 100644 intro_pytest/function.py create mode 100644 intro_pytest/tests/__init__.py create mode 100644 intro_pytest/tests/test_function.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8fbef9a --- /dev/null +++ b/.gitignore @@ -0,0 +1,142 @@ +# ---> Python +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols + +# linux +*.swp +*.swo diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..de0dfe2 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,25 @@ +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.2.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files + +- repo: https://github.com/ambv/black + rev: 21.7b0 + hooks: + - id: black + args: [--line-length=79] + language_version: python3.8 + +- repo: https://github.com/pycqa/flake8 + rev: 3.7.9 + hooks: + - id: flake8 + +- repo: https://github.com/pre-commit/mirrors-mypy + rev: v0.910 + hooks: + - id: mypy diff --git a/README.MD b/README.MD new file mode 100644 index 0000000..e69de29 diff --git a/intro_pytest/Pipfile b/intro_pytest/Pipfile index 22d660a..11c3029 100644 --- a/intro_pytest/Pipfile +++ b/intro_pytest/Pipfile @@ -6,6 +6,7 @@ name = "pypi" [packages] [dev-packages] +pytest = "*" [requires] python_version = "3.8" diff --git a/intro_pytest/Pipfile.lock b/intro_pytest/Pipfile.lock index e42812c..5f6bc5a 100644 --- a/intro_pytest/Pipfile.lock +++ b/intro_pytest/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "7f7606f08e0544d8d012ef4d097dabdd6df6843a28793eb6551245d4b2db4242" + "sha256": "44ccf4c66d663506b934edaa5a4873daba29d125557d68c7840956adb918c913" }, "pipfile-spec": 6, "requires": { @@ -16,5 +16,69 @@ ] }, "default": {}, - "develop": {} + "develop": { + "attrs": { + "hashes": [ + "sha256:149e90d6d8ac20db7a955ad60cf0e6881a3f20d37096140088356da6c716b0b1", + "sha256:ef6aaac3ca6cd92904cdd0d83f629a15f18053ec84e6432106f7a4d04ae4f5fb" + ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'", + "version": "==21.2.0" + }, + "iniconfig": { + "hashes": [ + "sha256:011e24c64b7f47f6ebd835bb12a743f2fbe9a26d4cecaa7f53bc4f35ee9da8b3", + "sha256:bc3af051d7d14b2ee5ef9969666def0cd1a000e121eaea580d4a313df4b37f32" + ], + "version": "==1.1.1" + }, + "packaging": { + "hashes": [ + "sha256:7dc96269f53a4ccec5c0670940a4281106dd0bb343f47b7471f779df49c2fbe7", + "sha256:c86254f9220d55e31cc94d69bade760f0847da8000def4dfe1c6b872fd14ff14" + ], + "markers": "python_version >= '3.6'", + "version": "==21.0" + }, + "pluggy": { + "hashes": [ + "sha256:4224373bacce55f955a878bf9cfa763c1e360858e330072059e10bad68531159", + "sha256:74134bbf457f031a36d68416e1509f34bd5ccc019f0bcc952c7b909d06b37bd3" + ], + "markers": "python_version >= '3.6'", + "version": "==1.0.0" + }, + "py": { + "hashes": [ + "sha256:21b81bda15b66ef5e1a777a21c4dcd9c20ad3efd0b3f817e7a809035269e1bd3", + "sha256:3b80836aa6d1feeaa108e046da6423ab8f6ceda6468545ae8d02d9d58d18818a" + ], + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", + "version": "==1.10.0" + }, + "pyparsing": { + "hashes": [ + "sha256:c203ec8783bf771a155b207279b9bccb8dea02d8f0c9e5f8ead507bc3246ecc1", + "sha256:ef9d7589ef3c200abe66653d3f1ab1033c3c419ae9b9bdb1240a85b024efc88b" + ], + "markers": "python_version >= '2.6' and python_version not in '3.0, 3.1, 3.2, 3.3'", + "version": "==2.4.7" + }, + "pytest": { + "hashes": [ + "sha256:131b36680866a76e6781d13f101efb86cf674ebb9762eb70d3082b6f29889e89", + "sha256:7310f8d27bc79ced999e760ca304d69f6ba6c6649c0b60fb0e04a4a77cacc134" + ], + "index": "pypi", + "version": "==6.2.5" + }, + "toml": { + "hashes": [ + "sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b", + "sha256:b3bda1d108d5dd99f4a20d24d9c348e91c4db7ab1b749200bded2f839ccbe68f" + ], + "markers": "python_version >= '2.6' and python_version not in '3.0, 3.1, 3.2, 3.3'", + "version": "==0.10.2" + } + } } diff --git a/intro_pytest/function.py b/intro_pytest/function.py new file mode 100644 index 0000000..b45dce7 --- /dev/null +++ b/intro_pytest/function.py @@ -0,0 +1,22 @@ +# function.py + + +class Maths: + """ + A Maths class + n (int) - starting interger value + """ + + def __init__(self, n: int): + self.n = n + + def add(self, x: int) -> int: + self.n += x + return self.n + + def subtract(self, x: int) -> int: + self.n -= x + return self.n + + def __repr__(self): + return f"n is {self.n}" diff --git a/intro_pytest/tests/__init__.py b/intro_pytest/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/intro_pytest/tests/test_function.py b/intro_pytest/tests/test_function.py new file mode 100644 index 0000000..4afc237 --- /dev/null +++ b/intro_pytest/tests/test_function.py @@ -0,0 +1,30 @@ +# test_function.py +import pytest + +from function import Maths + + +@pytest.fixture +def maths(): + return Maths(10) + + +def test_exists(maths): + """ + Checks if objects are created + """ + assert isinstance(maths, Maths) + + +def test_add(maths): + """ + Addition method + """ + assert maths.add(10) == 20 + + +def test_subtract(maths): + """ + Subtraction method + """ + assert maths.subtract(5) == 5