What's the best way to run unit tests against an ASP.NET REST Web API server RRS feed

  • Question

  • User-1608281605 posted

    We have an ASP.NET Web API server app that provides a REST API.

    What I want is pretty simple. That when our unit tests run, and I prefer NUnit but can do the Microsoft test framework, that it would fire up the server app. And I then have unit tests written that make REST calls and assert the returned response.

    Which leaves the questions:

    1. Is there a way when the tests start, to first get our server web app up and running on IIS Express. And not start the tests until it is running and available.
    2. Is there a way to either hard-code the port for the web app on IIS Express or to get the port for the unit tests?
    3. How do I get IIS Express to end my web app and then exit?
    4. To do this, do I then need all the tests in one suite so I'm not starting & stopping multiple instances of our web app?

    thanks - dave

    Thursday, April 16, 2020 11:03 PM

All replies

  • User1120430333 posted

    When is a test not a unit test?


    If you want to do integration testing of a WebAPI that's one thing, but a unit test of a Web service that's what a WebAPI is it is a Web service, I would have to question it.

    Friday, April 17, 2020 6:00 AM
  • User-1608281605 posted

    Sorry - integration tests. I use "unit tests" as shorthand for both since I use the same unit test framework for both.

    Friday, April 17, 2020 11:44 AM
  • User475983607 posted

    While I do not agree with this testing approach, I feel the best solution is deploying the Web API application to localhost IIS.  Then run the integration tests from localhost rather than IIS Express.  The deployment strategy is up to you.  For example, you could manually deploy a build or deploy the application every time there is a build.  See the project properties build events to move files to the configured application root directory.

    Anyway, the idea is localhost IIS is always running on the configured port so there is no need to start IIS.

    Friday, April 17, 2020 12:15 PM