'Testing web API service

I'd like to test the methods of a given Web API service. I could do this by adding a test project, instantiating the controllers and testing the methods. However, I don't want to just test the methods, I'd like to test the Request and Response. I do something similar in Java, like this:

  private HttpServer server;

    @Before
    public void before() {
        this.server = Servidor.initializeServer();
    }

    @After
    public void stopServer() {
        server.stop();
    }

    @Test
    public void testaQueBuscarUmCarrinhoTrazOCarrinhoEsperado() {
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target("http://localhost:8080");
        String conteudo = target.path("/carrinhos").request().get(String.class);
        Carrinho carrinho = (Carrinho) new XStream().fromXML(conteudo);
        Assert.assertEquals("Rua Teste", carrinho.getRua());
    }

Now, to do something like this I have to run my web API project to start the server, THEN run the tests. Is there a way of writing code for this?



Solution 1:[1]

yes you can do that , there is something called selfhosted web api see this given link you can build your test project on the same lines.

http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Prashant