Unit Testing Web API using HttpServer or HttpSelfHostServer
Asked Answered
C

1

11

I am trying to do some unit testing in for a Web API project. I am going simulate the web API hosting environment. It seems like that I could use In memory host (HttpServer) or self host (HttpSelfHostServer).

Just wondering what are the difference and which technology is good for what and is there any limitation for those options.

Crockett answered 5/2, 2013 at 0:18 Comment(0)
A
13

You should use in memory host for end-to-end tests and then test the network connectivity of your environment separately.

For a number of reasons:

  • In memory host, as the name suggests, runs entirely in memory so will be much faster

  • Self host needs to be run with elevated privileges, so your tests will need to be executed within the context of an "admin" identity. This is far from desired. It is especially troublesome if you want to execute tests from i.e. build scripts or from PowerShell, since, as a result, these processes would also have to be started with elevated privileges. Moreover, this will have to happen on any of the servers you test on.

  • In self host you end up testing the given operating system’s networking stack which really is something that shouldn't be tested – since it might differ across different environments (development, staging, QA, production and so on). For example - a given port might not be available. As a result you might get dragged into unnecessary debugging efforts across different machines to even get the tests running.

  • Finally, testing using self-hosting, still doesn't guarantee that the service will run correctly when web-hosted and vice versa - so you might as well just test in memory

Antilogism answered 5/2, 2013 at 0:45 Comment(3)
You do not need admin privileges always to run self host. You have to add config.HostNameComparisonMode = System.ServiceModel.HostNameComparisonMode.Exact; to run as non-admin if you are listening locally.Gist
Recommend to use Owin TestServer. link: juliencorioland.net/archives/…Photosynthesis
If you use HttpSelfHostServer you can write tests in a way that you just change the url and your test can run against your dev/stage servers.Headmistress

© 2022 - 2024 — McMap. All rights reserved.