Skip to content

Commit

Permalink
feat(hypothesis): Added hypothesis as an option (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayeankit authored Jun 29, 2023
1 parent 49acd3a commit 791ca16
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ for a Python package.
- Allows package slug (use `_` instead of `-`)
- Licenses supported: MIT, BSD 3 Clause, ISC License, Apache Software License 2.0, and GPL 3
- Documentation engines: mkdocs, sphinx, jupyter-boook
- Test library: pytest
- Test library: pytest, hypothesis
- Auto format code tool: blue and black
- Initial integration with git
- Support to conda (as base environment) and poetry as packaging and dependency management
Expand Down
8 changes: 8 additions & 0 deletions docs/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ most popular system compilers to suit your needs and preferences for developing
Python packages. If you think we should add more options, you can submit your
suggestion as a issue at
https://github.com/osl-incubator/scicookie/issues/new/choose.

## Test Library

There are several test library options available to development of Python packages. SciCookie support the following:

- [**Pytest**](https://docs.pytest.org/en/): is a popular testing framework for Python. It simplifies the process of writing and running tests by providing a concise syntax and powerful features. With Pytest, you can automatically discover and collect test cases, use fixtures for test setup and resource management, and write test functions with assert statements to check expected outcomes. It offers various options for test execution, including running specific tests, parallel execution, and generating test reports. Pytest also has a thriving ecosystem of plugins that extend its capabilities, such as code coverage analysis and test parameterization. Overall, Pytest is widely adopted for its simplicity, flexibility, and community support, making it an effective tool for ensuring the quality and reliability of Python code. You can check documentation [here](https://docs.pytest.org/en/)

- [**Hypothesis**](https://hypothesis.readthedocs.io/): is a property-based testing library for Python. It focuses on generating diverse input data and exploring different scenarios to thoroughly test code. Instead of relying on specific examples, Hypothesis allows you to define general properties that your code should satisfy. It automatically generates random inputs, including edge cases, to uncover potential bugs and unexpected behaviors. Hypothesis integrates well with popular testing frameworks like Pytest and promotes comprehensive testing to improve code reliability. You can check documentation [here](https://hypothesis.readthedocs.io/)
1 change: 1 addition & 0 deletions src/scicookie/cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"use_pre_commit": "yes",
"use_pydocstyle": "yes",
"use_pytest": "yes",
"use_hypothesis": "yes",
"use_shellcheck": "yes",
"use_vulture": "yes",
"use_containers": [
Expand Down
1 change: 1 addition & 0 deletions src/scicookie/profiles/base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ use_tools:
- pre-commit
- pydocstyle
- pytest
- hypothesis
- shellcheck
- vulture
enabled: false
Expand Down
10 changes: 7 additions & 3 deletions src/scicookie/{{cookiecutter.project_slug}}/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,15 @@ clean: ## remove build artifacts, compiled files, and cache
lint:
pre-commit run --all-files

{% if cookiecutter.use_pytest %}
.PHONY:test
.PHONY: test
test: ## run tests quickly with the default Python
{%- if cookiecutter.use_pytest == "yes" %}
pytest
{% endif %}
{%- elif cookiecutter.use_hypothesis == "yes" %}
python -m unittest discover
{%- else %}
@echo "No test library selected."
{%- endif %}

{% if cookiecutter.documentation_engine == 'mkdocs' -%}
.PHONY:docs-build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ pytest = "^7.3.2"
pytest-cov = "^4.1.0"
{% endif %}
{%- endif -%}{#- end of use_pytest -#}
{%- if cookiecutter.use_hypothesis == "yes" -%}
hypothesis = "^6.0"
{% endif %}
{%- if cookiecutter.use_coverage == "yes" -%}
coverage = "^7.2.7"
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,16 @@ Before you submit a pull request, check that it meets these guidelines:
## Tips

To run a subset of tests::

{% if cookiecutter.use_pytest == "yes" -%}
```
$ pytest tests.test_{{ cookiecutter.package_slug }}
```
{%- endif %}
{% if cookiecutter.use_hypothesis == "yes" -%}
```
python -m unittest discover
```
{%- endif %}

## Release

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
Tests for {{ cookiecutter.package_slug }} package.
"""
{% if cookiecutter.use_pytest == "yes" -%}
import pytest
{% endif -%}
{% if cookiecutter.use_hypothesis == "yes" -%}
from hypothesis import given
from hypothesis import strategies as st
{% endif -%}
{% if cookiecutter.use_pytest == "yes" %}

@pytest.fixture
def response_pytest():
"""Sample pytest fixture.
See more at:
http://doc.pytest.org/en/latest/fixture.html
"""
{% endif -%}
{% if cookiecutter.use_hypothesis == "yes" %}

@pytest.fixture
def response_hypothesis():
"""Sample pytest fixture.
See more at:
https://hypothesis.readthedocs.io/en/latest/quickstart.html
"""
{% endif -%}

{%- if cookiecutter.use_pytest == "yes" and
cookiecutter.use_hypothesis == "yes" %}

@given(st.text())
def test_content_hypothesis1():
"""Sample pytest test function with the
pytest fixture and hypothesis as arguments.
"""
# Test code using the response fixture and
# hypothesis_argument


@given(st.text())
def test_content_hypothesis2():
"""Sample pytest test function with the hypothesis
fixture as an argument.
"""
# Test code using the response fixture and
# hypothesis_argument
{% endif -%}
{#- keep this line at the end of the file -#}

This file was deleted.

0 comments on commit 791ca16

Please sign in to comment.