nestorsalceda / mamba

The definitive testing tool for Python. Born under the banner of Behavior Driven Development (BDD).
http://nestorsalceda.github.io/mamba
MIT License
518 stars 65 forks source link

Shared contexts #113

Closed kfischer-okarin closed 6 years ago

kfischer-okarin commented 6 years ago

I added two new example group contexts shared_context and included_context.

Any shared_context will not be executed on its own but will be inserted in any place where you use the context included_context with the same description .

Example:

with shared_context('Request tests'):
    with context('When the request succeeds'):
        with before.each:
            # Mocking success
        with it('returns HTTP Status Code 200'):
            ...
    with context('When the request fails'):
        ...

with describe('GET /api/important_data'):
    with before.all:
        # Some endpoint-specific preparations
    with included_context('Request tests'):
        pass

with describe('POST /api/dangerous'):
     with before.all:
        # Some endpoint-specific preparations
     with included_context('Request tests'):
        with it('additional security test'):
            pass

As demonstrated by the (maybe too simple?) tests, any examples defined inside the included_context will be added to those of the inserted shared context.

I didn't add any special support for several hooks or anything, since it shouldn't be necessary... with the current architecture of mamba, any hook defined inside included_context will take precedence over any hook defined in the inserted shared_context. If you want to combine hooks of the same kind (two before.each, two after.all), you can just wrap the included context in another context that defines the additional hooks.

This will resolve Issue #90 (which I submitted myself some months ago)

kfischer-okarin commented 6 years ago

@nestorsalceda Any thoughts/feedback on this? If this is a acceptable solution, then it would be nice to use it in production soon....

nestorsalceda commented 6 years ago

Hey @DerTraveler

This is an awesome work! Sorry for the delay, I messed with my notifications settings and didn't notice about this.

Let me to do some review, but I think it will be merged today.

Thanks another time!

nestorsalceda commented 6 years ago

It's merged! Thanks :metal: