Unit Testing Web API using HttpServer or HttpSelfHostServer

我是研究僧i 提交于 2019-12-12 07:49:31

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/14698130/unit-testing-web-api-using-httpserver-or-httpselfhostserver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!