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 can not be extracted into seperate files #128

Open Askir opened 5 years ago

Askir commented 5 years ago

As the title suggests I can't find a way to extract my shared_context blocks into different files, which makes the feature much less useful. I tried:

I'd be very happy to implement this myself. But I could use some pointers on where to start.

kfischer-okarin commented 5 years ago

With the current implementation using AST transformation this is quite difficult to do in a naive way as far as my understanding goes.

The resulting module (including the imports and everything) is not executed until the test is actually run. The Syntax parser actually doesn't know or care what is imported in the test files...

In my opinion, you would need a preliminary AST parsing before the actual AST transformation which collects all the shared_contexts which should be shared across files.

I see two methods to do that: 1) Add a new example group type called exported_context, which extends SharedExampleGroup. Parse all spec files for them and make them available globally.

  # test_one_spec.py
  with exported_context('Exported'):
      ...

  with describe(SomeClass):
      with included_context('Exported'):
          ...

  # test_two_spec.py
  with included_context('Exported'):
      ...

2) Add a new file suffix (like _context.py) for defining contexts shared across files and parse these files specifically for shared_contexts (no special context group needed I guess?) and share them globally.

  # exported_shared_context.py
  with shared_context('Exported'):
      ...

  # test_two_spec.py
  with included_context('Exported'):
      ...

Some problems I see that need to be solved will be:

@nestorsalceda I'm not sure if I'm totally off with something, maybe you see something different, too?

Askir commented 5 years ago

Thank you for the fast and detailed response.
After looking into the code and reading your reasoning it makes a lot of sense that this does not work in the current state. Never the less I think this would be a really nice feature.

I'd personally prefer your exported_context idea. As you might not want to have to extract every exported_context into another file immediately. Sometimes having one of those contexts in another spec file is perfectly reasonable.

And I have never worked with ast before but that wont keep me from trying. If you have any additional input, let me know.
Otherwise I'll just start coding and open a PR somewhat soon with my initial results so we have some code to talk about, that makes discussion much easier I think.