开发者

Remote unit testin of a WebService in Visual Studio 2010

开发者 https://www.devze.com 2023-04-12 13:48 出处:网络
I need to change my unit test from local to remote tests and so far I thought that all I had to do is change UrlToTest to point to another server... But VS keeps on insisting to create a Development W

I need to change my unit test from local to remote tests and so far I thought that all I had to do is change UrlToTest to point to another server... But VS keeps on insisting to create a Development Web Server instead of using the one that is already running.

So after reading some docs my question actually is do I have install Test Controller and Test Agent on both remote and local computer or what? What if the WebService is on Linux...

Note that I don't want to debug the application that I'm testing. I simply want tests to be executed for a WebService that is already running, that is deployed.

I probably should mention that all my tests consists of WebService calls and some checks like this:

    [TestMethod()]
    [HostType("ASP.NET")]
    [AspNetDevelopmentServerHost("MainProjectName", "/")]
    [UrlToTest("http://servername:port/websitename/TestingOnlyWebForm.aspx")]
    public void LoginEmptyDataTest()
    {
        IUserService userService = CreateIUserService();
        string email = "";
        string password = "";
      开发者_运维知识库  ReturnMessage<User> actual;
        actual = userService.Login(email, password);
        Assert.AreNotEqual(true, actual.Status);
        Assert.AreNotEqual("db_error", actual.Info);
    }

But I have also more complicated tests in which I change some data and send it to another WebService and so on.

Note that UrlToTest previously was pointing to localhost at which point it works but starts a developer server which is not what I want.


What you are trying to is not possible. All that generated unit test is trying to do is to run the test locally on the machine either using the development server by specifying AspNetDevelopmentServerHost or by using local IIS, when AspNetDevelopmentServerHost is not present.

If you want to test remote services right click your unit test project and add a service reference. Point to your service give it a namespace, say Services, and generate the proxies. Once you have the proxies generated just instantiate them and call the methods. Also remove all the unneeded attributes from your test. Your test should roughly look like this:

     [TestMethod]
        public void LoginEmptyDataTest()
        {
            using (var userServiceClient = new Services.UserServiceClient(
                                        "BasicHttpBinding_IUserService", 
                                        "http://someremotehost/userservice.svc"))
            {
                var result = userServiceClient.Login("user", "password");

                // asserts go here
            }
        }

This may solve your immediate problem however you should re-think what you are doing as @eglasius said. what happens if the code you call changes state internally? Next test might fail because of that so you need clean-up strategies otherwise your tests will be very brittle and you'll end up ignoring them.

Update: passing an address at run-time. Change the first parameter to whatever enpoint configuration name you have in your config file.


I'll take a stab in the dark at this one because I did something similar recently.

In my case my test project referenced the service project to provide visibility of the service and data contracts the Web Service implements and consumes.

To resolve this - though it can be ignored - move the contracts to a new project. Then have the service and test projects reference the new project, and remove the test projects' reference to the service.

Hope that makes sense!

Once the reference is removed, VS will no longer feel the need to start up your service when you run your tests.


You can disable the startup of the Service Host in the Project settings of your WCF Service Project. Right Click - WCF Options - Uncheck "Start WCF Service Host when debugging another project in the same solution".


You really have to consider the nature of what you're trying to achieve here.

It's hard to tell exactly what you're hitting of the code. I have the impression, you have is a website that calls a web service. You're testing the client code in that context, instead of just testing the service.

If that's the case, remove the attributes and point the url to the correct service like UrbaEsc guided you to. If you don't remove the attributes, you're running the calling code in the context of the site.

Even if the above is not the scenario, and based on what you replied to UrbanEsc in the comments, you'd then be testing an external call to the webservice initiated from the same site process.

You said: "Found it, but VS still starts something on localhost and then trys to reach external server... Seems like VS is just not designed for real remote testing of webservices"

Read the above. I'd say you need to better understand what you're enabling. You can certainly test remote web services, like you can do pretty much anything you can code. You do that from client code that knows nothing different that what any other external client of the service would know. Support doesn't stop there, as you can do load testing of the service.

Note that what you're doing aren't unit tests, these are integration tests. Or depending on the scope of your system, full system tests.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号