Comments (8)
I believe what you need is merge_context, and not nest_context: https://testslide.readthedocs.io/en/1.4.2/testslide_dsl/shared_contexts/index.html#sharing-contexts.
The docs can certainly be improved, but the gist is:
- merge_context, puts whatever is defined in the shared context, inside the context where it is being merged, including any hooks (before, after) and examples.
- nest_context, creates a new sub context, under the context where it is being nested, with the contents of the shared context.
Generally speaking:
- You merge contexts when you want common hooks to be used elsewhere (and perhaps also examples).
- You nest contexts when the same set of examples makes sense in different scenarios.
Makes sense?
from testslide.
I see. So nest context is not really a shared "context" but essentially a set of shared "hooks" + "examples". It can't host more examples, but it lives like a guest in other host contexts..
Then it's so confusing to name it as context, as from the API design, I feel a context should always be an environment that can host examples.
from testslide.
I agree, this is far from obvious. RSpec (which heavily inspired the DSL) calls them shared examples (and include_examples
) and shared contexts (and include_context
). How about using the same naming scheme?
from testslide.
It's better anyway.
I think "merge_context" is good and easy to understand.
"nest_context" would be better to be called "include_context".
"Include" sounds more like reusing a of piece of code, indicating what it does clearly.
from testslide.
There is the confusion to expect "merge_context" and "nest_context" behave similarly, because they are both derived from "shared_context".
I hope the concept of "shared_context" could be taken down (as #13 suggested), so users will not have this assumption.
from testslide.
I have a new idea about this issue.
I realized that the problem is actually that when I want to re-use a context, what parts of the context I want to bring in.
A context has two parts basically,
- one is for setting up some constants, helper function and mocks,
- the other part is the examples enclosed in the context.
Instead of having two decorator "merge_context" and "nest_context" that users have to read doc to understand their differences.
I think it's better to just provide a single decorator but with arguments allowing users to choose what to bring in, the setups or the examples or all.
from testslide.
That makes sense. But there's another aspect of that as well. Sometimes you actually want to merge another context hooks / examples at the same level, and other times, you want to create another sub_context, and put it there. If we don't provide that, the latter case would require defining a sub_context, and then merging it there. Having nest_context is a convenience.
Summarizing things that were said:
- Contexts are a gorup of hooks and/or examples.
- They can be "real", meaning its examples will be executed, or "virtual", that examples are not executed.
- "virtual" contexts are useful for importing its hooks and/or examples into another context (code sharing).
- Potentially, this can happen in a new sub_context for convenience.
(I said "real" and "virtual" here to avoid biasing towards names)
Question now is: what naming / interface would make all of this clear?
Looking back at RSpec's naming scheme, shared_examples can actually... contain hooks as well, which can be misleading...
From your suggestion, I suppose you meant something like this:
context.include_shared_context(
nested=True/False,
examples=True/False,
hooks=True/False,
)
Which seems a bit cumbersome... not to mention, that if you pull examples from a context, you must pull the hooks as well... And if you pull just hooks, it can be a bit confusing...
I feel like we're prematurely going after problems that are not necessarily here yet.
My suggestion to close this issue is:
- Keep the current model, as it seems to cover all use cases.
- Find more appropriate names for the DSL methods.
- Update the docs.
Fair?
from testslide.
I updated the docs to make things clearer. I'm closing this issue now, as it seems to be mostly a documentation issue than anything else. Feel free to ping back if you find a working solution I haven't seen.
Thanks for the report!
from testslide.
Related Issues (20)
- Update documentation screeenshots & output examples HOT 2
- Add linter to ensure Copyright header on files
- BUG: Leaked task detection triggers for cancelled tasks HOT 2
- Context methods can't be async and are being quietly ignored
- StrictMock validations broken when used via inheritance.
- Improve Coroutine value error error message
- patch_attribute issue with Python 3.10 HOT 1
- Testslide runner not playing well with unittest.subtests HOT 1
- [RFC] [runner] Use Pattern.search() instead of Pattern.match() for "--filter-regex" behavior HOT 1
- Make Gitlab CI running tests/linting also for pull requests. HOT 1
- add assert_called_times(times:int) for callable mocks HOT 1
- `and_assert_not_called` shouldn't need any behaviour defined beforehand HOT 2
- Make to_return_values work for patch attribute HOT 1
- Cannot mock method with signature `def ...(...) -> Awaitable[...]` due to TypeCheckError
- Windows support HOT 1
- Cannot type check with `from __future__ import annotations`
- Can't mock a @property without calling it? HOT 5
- AttributeError for asyncio.coroutines.CoroWrapper in Python 3.11.0 HOT 1
- Test failures with Pygments 2.14
- Request: Container Matchers HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from testslide.