Skip to content
This repository has been archived by the owner on Jun 12, 2021. It is now read-only.

[1.x only] An instance of the test class is created for every step #187

Closed
georgehemmings opened this issue Dec 1, 2014 · 7 comments
Closed
Labels
bug Something isn't working wontfix This will not be worked on

Comments

@georgehemmings
Copy link

It seems that an instance of the test class is created for every step in the scenario. Is this by design? Is it avoidable in anyway?

I've moved the constructor code in to a Background method to minimize the impact of this.

@adamralph
Copy link
Owner

That is not by design. IIRC, a new instance should be created, at most, per
scenario and all steps should be executed in that context. I'll have to
check the code for an exact answer.
On 1 Dec 2014 18:29, "George Hemmings" notifications@github.com wrote:

It seems that an instance of the test class is created for every step in
the scenario. Is this by design? Is it avoidable in anyway?


Reply to this email directly or view it on GitHub
#187.

@georgehemmings
Copy link
Author

FYI, I'm using xbehave 1.1.0 and the ReSharper runner 1.6.2

@adamralph
Copy link
Owner

I've done some investigation and it looks like you're correct, the xUnit.net runners are creating an instance of the class each time a step is executed. However, before that happens, xBehave.net creates an initial instance of the class and runs all the steps in the context of that initial instance. The other instances which are created by the xUnit.net runners are superfluous.

I guess the reason that no-one has noticed this before is because, generally, the creation of an instance of the test class has no static side effects. I suppose in your case you have some static side effects which means the instances are interfering with one another.

I've also confirmed that this behaviour does not occur in xBehave.net 2.0 (currently under development). xBehave.net 2.0 is based on xUnit.net 2.0, and both are complete re-writes.

Is it possible for you to refactor your tests to remove the static side effects of multiple instances? Generally, if you can avoid using constructors and instead rely on [Background] to set up a common context for each scenario then you should be OK.

@adamralph adamralph added bug Something isn't working 1 - Spike and removed 1 - Spike labels Dec 1, 2014
@adamralph
Copy link
Owner

Sorry, I just read back to your initial report and saw that you've done exactly what I've suggested and moved your common context from constructor to background. Is that a workable solution for you for the time being? Given that this bug has gone away in 2.0 I'd prefer not to spend time fixing it in 1.x if possible.

2.0 should go into beta before the end of the year and will have a stable release soon after xUnit.net 2.0 is stable.

@georgehemmings
Copy link
Author

Thanks for looking into this. Using [Background] is fine. It was more of a performance concern than anything else. I was introduced to xbehave via the Web API book, for every step they will be creating a HttpServer. https://github.com/webapibook/issuetracker/blob/BuildingTheApi/test/WebApiBook.IssueTrackerApi.AcceptanceTests/HomeFeature.cs

I liked the idea of using a constructor as I initially saw it more like cucumber's before hook. However as xbehave doesn't report the step in the test output it's not really a problem.

@adamralph adamralph added wontfix This will not be worked on and removed 0 - Backlog labels Dec 1, 2014
@adamralph
Copy link
Owner

Thanks very much for raising it and bringing it to my attention. It's good to know for future reference.

@adamralph
Copy link
Owner

adamralph commented Dec 1, 2014

Added to known issues for 1.x https://github.com/xbehave/xbehave.net/wiki/Known-Issues "A test class instance is created for each step"

@adamralph adamralph changed the title An instance of the test class is created for every step [1.x only] An instance of the test class is created for every step Nov 28, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants