Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Initial attempt at implementing property-based testing using Hypothesis #4724

Merged
merged 34 commits into from
Feb 24, 2025

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)

Starts #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.

@@ -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)

@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 and others added 11 commits January 1, 2025 04:32
Co-authored-by: Pradyot Ranjan <99216956+prady0t@users.noreply.github.com>
Co-authored-by: Pradyot Ranjan <99216956+prady0t@users.noreply.github.com>
Co-authored-by: Pradyot Ranjan <99216956+prady0t@users.noreply.github.com>
Co-authored-by: Pradyot Ranjan <99216956+prady0t@users.noreply.github.com>
Co-authored-by: Pradyot Ranjan <99216956+prady0t@users.noreply.github.com>
Co-authored-by: Pradyot Ranjan <99216956+prady0t@users.noreply.github.com>
Co-authored-by: Pradyot Ranjan <99216956+prady0t@users.noreply.github.com>
Co-authored-by: Pradyot Ranjan <99216956+prady0t@users.noreply.github.com>
Co-authored-by: Pradyot Ranjan <99216956+prady0t@users.noreply.github.com>
Co-authored-by: Pradyot Ranjan <99216956+prady0t@users.noreply.github.com>
@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 98.70%. Comparing base (725a42c) to head (6c8d2fc).
Report is 4 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #4724   +/-   ##
========================================
  Coverage    98.70%   98.70%           
========================================
  Files          304      304           
  Lines        23452    23483   +31     
========================================
+ Hits         23149    23180   +31     
  Misses         303      303           

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

@@ -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.

RohitP2005 and others added 5 commits January 8, 2025 06:51
Co-authored-by: Pradyot Ranjan <99216956+prady0t@users.noreply.github.com>
Co-authored-by: Pradyot Ranjan <99216956+prady0t@users.noreply.github.com>
Co-authored-by: Pradyot Ranjan <99216956+prady0t@users.noreply.github.com>
Co-authored-by: Pradyot Ranjan <99216956+prady0t@users.noreply.github.com>
Co-authored-by: Pradyot Ranjan <99216956+prady0t@users.noreply.github.com>
@RohitP2005
Copy link
Contributor Author

Hey @agriyakhetarpal can u trigger the CI workflows for this branch

@kratman
Copy link
Contributor

kratman commented Feb 10, 2025

@RohitP2005 Triggered CI and updated the branch

@Saransh-cpp Saransh-cpp requested a review from prady0t February 13, 2025 00:28
@Saransh-cpp
Copy link
Member

Requesting a review from @prady0t on this.

@RohitP2005
Copy link
Contributor Author

@RohitP2005 Triggered CI and updated the branch

thanks @kratman
i will look into the failing docs build asap

@RohitP2005
Copy link
Contributor Author

Hey @agriyakhetarpal I need updates on this PR. Its been there for a long time.
Looking forward to complete this and move on to the next one.

Copy link
Member

@agriyakhetarpal agriyakhetarpal left a comment

Choose a reason for hiding this comment

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

Done. Sorry for the delay in the review, @RohitP2005! Thanks for your work here. I just have one comment – happy to approve after that.

@agriyakhetarpal
Copy link
Member

Hi @prady0t, I plan to merge this early next week so that @RohitP2005 can proceed further – unless you have comments by then.

@RohitP2005
Copy link
Contributor Author

@agriyakhetarpal I would like a final check by @prady0t
before merging this.
Cause there's lot other tests that need property based testing.
It would helpful if you suggest what to do next on this matter.

@kratman
Copy link
Contributor

kratman commented Feb 22, 2025

Cause there's lot other tests that need property based testing. It would helpful if you suggest what to do next on this matter.

I think I would like to see this be limited to only where it is needed and useful rather than all tests. We would not want to cause the complexity and runtime of our tests to explode from excessive testing. The functions you have done so far are fairly straight forward, but tests in other parts of the code can be quite slow and complicated.

My other concern is that the codebase is physics based, so you need to make sure that the value ranges make sense for the codebase as well. Some parameters may only be valid in the 0-1 range or need to be only positive floats.

@prady0t
Copy link
Contributor

prady0t commented Feb 24, 2025

Other than the above review, everything look good. Thanks @RohitP2005

@agriyakhetarpal agriyakhetarpal enabled auto-merge (squash) February 24, 2025 20:43
@agriyakhetarpal agriyakhetarpal changed the title Implementing Property Based Tesing using Hypothesis Initial attempt at implementing property-based testing using Hypothesis Feb 24, 2025
@agriyakhetarpal agriyakhetarpal merged commit e69fc9a into pybamm-team:develop Feb 24, 2025
26 checks passed
@RohitP2005 RohitP2005 deleted the issue#4703 branch February 25, 2025 03:07
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants