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

Allow for unit tests without decorators #2515

Merged
merged 7 commits into from
Jan 4, 2021

Conversation

chusloj
Copy link
Contributor

@chusloj chusloj commented Dec 7, 2020

This PR addresses a problem that @jdebacker had with creating unit tests for calcfunctions.py without decorators (#2489).

Using pytest with functions in calcfunctions.py is possible if the iterate_jit decorator is removed, so the decorator will now simply return the passed function with no wrapper if the OS environment variable for TESTING is set to 'True' (as a string, the environment variable only accepts strings) in test_calcfunctions.py.

The test function test_DependentCare() provided in #2489 passes with this change.


This also enables unit testing discussed in #2419.

@chusloj chusloj changed the title Allow for tests without decorators Allow for unit tests without decorators Dec 7, 2020
@jdebacker
Copy link
Member

Love this - thanks @chusloj !

@chusloj
Copy link
Contributor Author

chusloj commented Dec 7, 2020

@jdebacker,

I've found that changing the TESTING environment variable in test_calcfunctions.py apparently changes the variable for the entire test suite which causes problems. I've used monkeypatch to change the environment variable for that single function, but this means that any unit test made for calcfunctions.py will need a monkeypatch to change the environment variable. See below.

@hdoupe
Copy link
Collaborator

hdoupe commented Dec 7, 2020

@chusloj this is really elegant! One thing to make it easier is adding a fixture that does monkeypatch.setenv("TESTING", "True")

import pytest

@pytest.fixture
def skip_jit(monkeypatch):
    monkeypatch.setenv("TESTING", "True")
    yield

def test_DependentCare(skip_jit):
    """
    Tests the DependentCare function
    """
    test_tuple = (3, 2, 100000, 1, [250000, 500000, 250000, 500000, 250000], 
                  .2, 7165, 5000, 0)
    test_value = calcfunctions.DependentCare(*test_tuple)
    expected_value = 25196

    assert np.allclose(test_value, expected_value)

The skip_jit function would go into conftest.py.

@chusloj
Copy link
Contributor Author

chusloj commented Dec 7, 2020

@hdoupe Thanks very much for that suggestion! I was wondering how to fit the monkeypatch into a fixture but couldn't find the docs for how to implement a temporary change to a function using a fixture.

@hdoupe
Copy link
Collaborator

hdoupe commented Dec 7, 2020

No problem @chusloj!

@MattHJensen
Copy link
Contributor

MattHJensen commented Jan 4, 2021

Thanks for improving our testing capabilities, @chusloj! Merging now.

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.

4 participants