mamba
mamba copied to clipboard
Shared Contexts can not be extracted into seperate files
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:
- seperate file -> import that file ->
included_context
which throws a KeyError as the included_context can'tfind the extracted context.
I'd be very happy to implement this myself. But I could use some pointers on where to start.
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:
- 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'):
...
- 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:
- the handling of imports/dependencies of the imported shared context...
- Properly assigning the execution context (
self
) of the shared context
@nestorsalceda I'm not sure if I'm totally off with something, maybe you see something different, too?
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.