Does any one know if it is possible to host multiple instances of WebApplicationFactory<TStartop>()
in the same unit test?
I have tried and can't seem to get anywhere with this one issue.
i.e
_client = WebHost<Startup>.GetFactory().CreateClient();
var baseUri = PathString.FromUriComponent(_client.BaseAddress);
_url = baseUri.Value;
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Bearer", "Y2E890F4-E9AE-468D-8294-6164C59B099Y");
WebHost
is just a helper class that allows me to build factory and then a client easily in one line.
Under the covers all it does is this:
new WebApplicationFactory<TStartup>()
but a few other things too.
It would be nice if i could stand up another instace of a different web server to test server to server functionality.
Does anyone know if this is possible or not?
No. It's not possible. WebApplicationFactory
leans on xUnit's IClassFixture
, which has to be applied at the class level, meaning you only get one bite at the apple. The WebApplicationFactory
itself is capable of being customized per test, which fulfills most use cases where you're need a "different" one, but it doesn't help you wanting two totally separate active test servers at the same time.
However, that said, what you're wanting is a bad test design in the first place. The whole point of testing is to eliminate variables so you can actually ensure the piece of the SUT is actually working. Even in an integration testing environment, you're still just looking at one particular interaction between pieces of your application. Have two test servers, feeding off each other, effectively multiplies the variables giving you no assurance that either side is working correctly.
Fair enough - i have two webapis one acts like a gatekeeper to the other and sends requests for looks ups and etc before letting a request through .... so in essence i am workig in the same domain and testing one peice of functionality. I see your point but for me i do need two test servers really. I will have to resort to mocking the call out to the other server! Is was a stretch tbh
That still doesn't necessitate two servers at once. Any app that depends on your "gatekeeper" service could care less that it's proxying requests to another service. They just need a response and that comes from the gatekeeper. As a result, tests for those only involve the gatekeeper service, not your other service. Then, all you need is tests for the gatekeeper service itself to ensure that it's proxies the requests correctly to the other service. In each case, a single test server will do.
Hello again, @ChrisPratt. What you're doing here is applying unit testing principles to integration testing. The isolation you're suggesting is precisely what unit testing is for and precisely what integration testing is not for. Integration testing is the intentional testing against the underlying resources, why wouldn't this apply to other services?
@jefffischer: Incorrect. Integration testing is about literally testing the integration between your various units. What you're describing is a systems test, and is totally different.
I see what you're saying, but from what I've seen the connotation of "integration testing" has been replaced with functional testing on pretty much every team I've ever been on. You're saying your teams do not use integration testing to mean functional testing? Although I disagree with the terminology, that's all I've ever seen, @ChrisPratt.