-
Notifications
You must be signed in to change notification settings - Fork 722
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
FixtureLifeCycle not applied correctly to nested test classes #3888
Comments
From #3844 (comment):
|
Unfortunately, there is a more fundamental problem with applying the life cycle for nested fixtures due to current implementation: Here's the example: [FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
public class MainFixture
{
[FixtureLifeCycle(LifeCycle.SingleInstance)]
public class NestedFixture
{
}
} Now, you have to check at a later time (the fixtures have already been built, so no more attributes) whether the NestedFixture.LifeCycle equals LifeCycle.InstancePerTestCase. You check the fixture - it has SingleInstance. But is it genuine SingleInstance or the one inherited from parent? We don't know and cannot make a reliable decision. To fix this we should introduce a new value into LifeCycle enum (Unset / Default / Inherit), but that would be a breaking change, I guess. |
Hi @gleb-osokin, I think this is fine in practice as the inheritance (of assembly level vs testcase level) is applied in TextFixtureAttribute.BuildFrom: public IEnumerable<TestSuite> BuildFrom(ITypeInfo typeInfo, IPreFilter filter)
{
[...]
fixture.ApplyAttributesToTest(new AttributeProviderWrapper<FixtureLifeCycleAttribute>(typeInfo.Type.GetTypeInfo().Assembly));
fixture.ApplyAttributesToTest(typeInfo.Type.GetTypeInfo()); i.e. the assembly attributes are explicitly applied to the testcase, rather than being resolved at some later level. I initially thought this was the role of |
@mvdfugro I guess I didn't express that in a right way: you don't even need the assembly-level attribute, just a simple nested TextFixture is enough to demonstrate the ambiguity (see my example above): |
So.. here's where I'm confused. In the current code base, the following works as expected:
In TestExtensions.HasLifeCycle, the following situation occurs:
The corresponding test case without attribute therefore doesn't work (same story: fixture.LifeCycle is SingleInstance as that's the default, and parent is null so there's no inheritance). What I'm confused by is how ITest.Parent is supposed to work -- from what I can find in debugging I get the impression that:
and the parent class is not a 'parent' in the So: There are currently two places where the Fixture + Assembly attributes are applied:
Long story short: I think
Alternatively, the internal logic that builds the .Parent hierarchy could be adapted such that there can be multiple fixture parents (i.e., we get Test -> InnerClass -> OuterClass -> Namespace). |
Found during work on #3844.
Reproduction testcase:
Expected result: Test passes
Actual result:
Verified on 3.13.1 and 'latest master' (5093879)
The text was updated successfully, but these errors were encountered: