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

Implementing Property Based Tesing using Hypothesis #4724

Open
wants to merge 25 commits into
base: develop
Choose a base branch
from

Conversation

RohitP2005
Copy link
Contributor

@RohitP2005 RohitP2005 commented Dec 30, 2024

Description

This change introduces the use of Hypothesis-based testing in two files and is being applied to selective functions as requested by the author and members of the issue #4703 (comment)

Fixes #4703

Type of change

Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #) - note reverse order of PR #s. If necessary, also add to the list of breaking changes.

  • New feature (non-breaking change which adds functionality)
  • Optimization (back-end change that speeds up the code)
  • Bug fix (non-breaking change which fixes an issue)

Key checklist:

  • No style issues: $ pre-commit run (or $ nox -s pre-commit) (see CONTRIBUTING.md for how to set this up to run automatically when committing locally, in just two lines of code)
  • All tests pass: $ python -m pytest (or $ nox -s tests)
  • The documentation builds: $ python -m pytest --doctest-plus src (or $ nox -s doctests)

You can run integration tests, unit tests, and doctests together at once, using $ nox -s quick.

Further checks:

  • Code is commented, particularly in hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

@prady0t
Copy link
Contributor

prady0t commented Dec 31, 2024

@RohitP2005 Add hypothesis as a dependency in pyproject.toml for checks to pass.

Here:

dev = [

Copy link
Contributor

@prady0t prady0t left a comment

Choose a reason for hiding this comment

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

I've added reviews for test_averages.py. I'll add reviews for test_unary_operators.py once the failing tests are fixed.

tests/unit/test_expression_tree/test_averages.py Outdated Show resolved Hide resolved
tests/unit/test_expression_tree/test_averages.py Outdated Show resolved Hide resolved
tests/unit/test_expression_tree/test_averages.py Outdated Show resolved Hide resolved
tests/unit/test_expression_tree/test_averages.py Outdated Show resolved Hide resolved
tests/unit/test_expression_tree/test_averages.py Outdated Show resolved Hide resolved
tests/unit/test_expression_tree/test_averages.py Outdated Show resolved Hide resolved
tests/unit/test_expression_tree/test_averages.py Outdated Show resolved Hide resolved
@@ -267,8 +271,9 @@ def test_r_average(self):
assert pybamm.r_average(a + b) == pybamm.r_average(a) + pybamm.r_average(b)
assert pybamm.r_average(a - b) == pybamm.r_average(a) - pybamm.r_average(b)

def test_yz_average(self):
a = pybamm.Scalar(1)
@given(st.integers(min_value=-(2**63), max_value=2**63 - 1))
Copy link
Contributor

Choose a reason for hiding this comment

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

Same question here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

explained here at #4724 (comment)

tests/unit/test_expression_tree/test_averages.py Outdated Show resolved Hide resolved
tests/unit/test_expression_tree/test_averages.py Outdated Show resolved Hide resolved
@prady0t
Copy link
Contributor

prady0t commented Dec 31, 2024

Also, fix the PR heading. It's not proper.

@RohitP2005 RohitP2005 changed the title Added hypothesis test to two unit testing Implementing Property Based Tesing using Hypothesis Dec 31, 2024
@RohitP2005
Copy link
Contributor Author

RohitP2005 commented Dec 31, 2024

I've added reviews for test_averages.py. I'll add reviews for test_unary_operators.py once the failing tests are fixed.

@prady0t
I've updated the heading as requested, and committed all your suggested changes except for one.
Regarding the implementation of Hypothesis, I limited its use to specific tests to avoid adding overhead by applying it to too many cases. Do you think the way I implemented Hypothesis is optimized, or are there areas where I can improve further?

Copy link

codecov bot commented Jan 2, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.22%. Comparing base (a7253b8) to head (007045e).
Report is 17 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #4724      +/-   ##
===========================================
- Coverage    99.22%   99.22%   -0.01%     
===========================================
  Files          303      303              
  Lines        23070    23101      +31     
===========================================
+ Hits         22891    22921      +30     
- Misses         179      180       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

.gitignore Outdated Show resolved Hide resolved
pyproject.toml Outdated Show resolved Hide resolved
@@ -174,9 +177,10 @@ def test_x_average(self):
assert pybamm.x_average(a + b) == pybamm.x_average(a) + pybamm.x_average(b)
assert pybamm.x_average(a - b) == pybamm.x_average(a) - pybamm.x_average(b)

def test_size_average(self):
@given(st.integers(min_value=-(2**63), max_value=2**63 - 1))
Copy link
Contributor

Choose a reason for hiding this comment

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

I see. Having large-sized integers causes buffer overflow (probably because of different ways int is handled by pybamm and numpy ). Yes it's good to restrict the range here.

tests/unit/test_expression_tree/test_unary_operators.py Outdated Show resolved Hide resolved
tests/unit/test_expression_tree/test_unary_operators.py Outdated Show resolved Hide resolved
tests/unit/test_expression_tree/test_unary_operators.py Outdated Show resolved Hide resolved
tests/unit/test_expression_tree/test_unary_operators.py Outdated Show resolved Hide resolved
tests/unit/test_expression_tree/test_unary_operators.py Outdated Show resolved Hide resolved
Comment on lines +126 to +129
random_value=st.integers(),
random_matrix=st.lists(
st.floats(min_value=-10, max_value=10), min_size=5, max_size=5
),
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we increase the size here? i'll look into this bit later

Copy link
Contributor Author

@RohitP2005 RohitP2005 Jan 8, 2025

Choose a reason for hiding this comment

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

okay , i guess 10 will be good a number

tests/unit/test_expression_tree/test_unary_operators.py Outdated Show resolved Hide resolved
@kratman
Copy link
Contributor

kratman commented Jan 10, 2025

@RohitP2005 you keep merging develop into this branch, but there are still a bunch of failing tests. Can you fix those so the review can continue

@RohitP2005
Copy link
Contributor Author

@RohitP2005 you keep merging develop into this branch, but there are still a bunch of failing tests. Can you fix those so the review can continue

Sorry about that, my previous commit passed the CI test, after the new branch merge it is failing.

I will look into it asap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use hypothesis to implement property based testing.
3 participants