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

WHAT-IF style testing #39

Open
brodybits opened this issue Feb 7, 2024 · 0 comments
Open

WHAT-IF style testing #39

brodybits opened this issue Feb 7, 2024 · 0 comments

Comments

@brodybits
Copy link
Owner

I had discovered some Python BDD-style testing resources but nothing seemed to be maintained:

So I figured out an easy way to do BDD-style testing with help from nullcontext:

from contextlib import nullcontext as given
from contextlib import nullcontext as when
from contextlib import nullcontext as then
from contextlib import nullcontext as and_

with given('two values 2 and 3'):
    x = 2
    y = 3

    with when('add the two values'):
        result = x + y

        with then('the result is 5'):
            assert result == 5

        with and_('the result is an integer'):
            assert isinstance(result, int)

    with when('get absolute difference between the two values'):
        result = abs(x - y)

        with then('the result is 1'):
            assert result == 1

Looks nice, but think it could be simpler.

In many cases we need unit testing to test not only the behavior we were looking for but what happens in case of anomalies such as bad input, keyboard interrupt, or I/O error in the middle?

Quick & simple lawyer-speak: What if ...?

I would also like to get rid of the ugly _ character from the and_ alias, if possible.

Came up with this:

from contextlib import nullcontext as WHAT_IF
from contextlib import nullcontext as THEN
from contextlib import nullcontext as AND

with WHAT_IF('add two values 2 and 3'):
    result = 2 + 3

    with THEN('the result is 5'):
        assert result == 5

    with AND('the result is an integer'):
        assert isinstance(result, int)

with WHAT_IF('get absolute the difference between 2 and 3'):
    result = abs(2 - 3)

    with THEN('the result is 1'):
        assert result == 1

I think using ALL-CAPS can be justified as this is using aliases, in a similar fashion to C-style #define or top-level constants, also helps designate important words (or key words with a space, not keywords in terms of parsing) for the reader. In other words, nice clarity here.

Alternatives could be to use "also" instead of "and", only capitalize the first letter ("And"), put underscore at the beginning (with _and(...)). I think using ALL-CAPS is the clearest & easiest.

And one more idea as a bonus encore:

from contextlib import nullcontext

WHAT_IF = nullcontext
THEN = nullcontext
AND = nullcontext

with WHAT_IF('add two values 2 and 3'):
    result = 2 + 3

    with THEN('the result is 5'):
        assert result == 5

    with AND('the result is an integer'):
        assert isinstance(result, int)

with WHAT_IF('get absolute the difference between 2 and 3'):
    result = abs(2 - 3)

    with THEN('the result is 1'):
        assert result == 1

This should probably be published as a library, maybe with some extra fancy features.

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

No branches or pull requests

1 participant