Why doesn't tracing behavior appear to work when added to Autofixture Fixture.Behaviors in an AutoDataAttribute implementation? -
when add new tracingbehavior
autofixture fixture
instance using
fixture.behaviors.add(new tracingbehavior());
i tracing output in r# unit test sessions window.
however, when inherit autodataattribute
, add behavior inside attribute's constructor this
i no tracing output, though tests indicate tracingbehavior
has been added fixture.behaviors
in both scenarios. below 2 simple tests , attribute implementation reproduce.
public class tracingbehaviortests { [theory, autodata] public void tracingoutputdisplayedwhenmanuallyaddedtofixture(fixture fixture) { fixture.behaviors.add(new tracingbehavior()); var actual = fixture.behaviors.oftype<tracingbehavior>().any(); assert.equal(true, actual); // passes fixture.create<string>(); // tracing output displayed } [theory, tracingfixtureconventions] public void tracingoutputdisplayedwhenusingtracingfixtureconventionsattribute(fixture fixture) { var actual = fixture.behaviors.oftype<tracingbehavior>().any(); assert.equal(true, actual); // passes fixture.create<string>(); // no tracing output displayed } } public class tracingfixtureconventionsattribute : autodataattribute { public tracingfixtureconventionsattribute() : base(new fixture()) { this.fixture.behaviors.add(new tracingbehavior()); } }
i had little digging figure out why happening.
the reason you're seeing behaviour when use default constructor of tracingbehavior
, console.out
destination.
when executing test case, xunit.net test runner assigns another textwriter
console.out
.
this happens before code block in test method executed, explains why first test in op displays trace: when tracingbehavior
instance created, console.out
already captured textwriter
.
however, in second test (using [tracingfixtureconventions]
attribute), console.out
has not yet been replaced when attribute's constructor runs. so, when test runs, traces the original console.out
, since there's no console present, traces void.
you can verify both test cases do log logging file instead:
public class tracingbehaviortests { [theory, autodata] public void tracingoutputdisplayedwhenmanuallyaddedtofixture(fixture fixture) { using (var tw = file.appendtext(path.combine(environment.currentdirectory, "log.txt"))) { tw.autoflush = true; fixture.behaviors.add(new tracingbehavior(tw)); var actual = fixture.behaviors.oftype<tracingbehavior>().any(); assert.equal(true, actual); // passes fixture.create<string>(); // tracing output logged file } } [theory, tracingfixtureconventions] public void tracingoutputdisplayedwhenusingtracingfixtureconventionsattribute(fixture fixture) { var actual = fixture.behaviors.oftype<tracingbehavior>().any(); assert.equal(true, actual); // passes fixture.create<string>(); // tracing output logged file } } public class tracingfixtureconventionsattribute : autodataattribute { public tracingfixtureconventionsattribute() : base(new fixture()) { var tw = file.appendtext(path.combine(environment.currentdirectory, "log.txt")); tw.autoflush = true; this.fixture.behaviors.add(new tracingbehavior(tw)); } }
the above 'fix' rather fragile, because (as written here) [tracingfixtureconventions]
attribute doesn't dispose of tw
, if test adorned [tracingfixtureconventions]
attribute runs first, log file in use, , other test not able write it.
for now, i'll leave exercise reader figure out how make file logging more robust, if that's cares about. personally, use autofixture tracing troubleshoot, , turn off again once i'm done troubleshooting.
Comments
Post a Comment