How to get code coverage from Web Tests?
Hi,
I'd like to get code coverage information from Web Tests. I configure the testo configuration in order to instrument the Web Site. I Run my web test but no coverage information was generated.
Is there any additional Step?
Thanks
[251 byte] By [
LucasC] at [2008-2-15]
Lucas,
I don't have an answer for you, but I ran into a similar problem. Hopefully two of us having a similar problem will attract the attention from somebody at Microsoft.
I'm having the same issue with a Web Service. I added a Web reference to my unit test DLL. I gave me the option to instrument the DLL in its original location (virtual directory under IIS). I reset IIS, and instrumented the code from within the test manager. That produced no output.
I then instrumented the code using vsinstr and started the monitor with vsperfmon before running the unit test. It created a coverage file alright, but when I opened it for analysis, contained no data.
What's the trick to getting this to work with web stuff?
I tried to figure out this yesterday but build I had some different issue that prevented me to run web tests. I sent an e-mail to get some answer to original post but I haven't got response yet. I will update it when I get answer.
Generally to collect code coverage you need to tell to replace binaries you are collecting code coverage with instrumented bits. For binaries to be intrumented successfully for code coverage they also has to have symbol information or symbol files. Did you have symbol files or did you get any errors running vsinstr tool?
Thanks,
Bata
A few question regarding what you are trying to do:
- What do you mean by "adding a web reference to the unit test DLL"? Do you mean you added the test project to the references to the web site?
- "It gave me the option to instrument the DLL in its original location" - what dialog gave you such an option and what did you do to bring up the dialog?
- How did you instrument the code from test manager?
NOTE that even if you instrument the binaries manually, ASP is going to recompile and will replace your instrumented binaries with uninstrumented binaries. So you will not get any data. This is probably what you are seeing. To get data with ASP projects, you need to configure it to perform instrumentation after the compilation step.
How can I do that? ("perform instrumentation after the compilation step")
Thanks
The reason I asked you the few questions I asked is to understand exactly how you are compiling and launching the ASP web page.
When browsing to a dynamic web page like ASP, the code is compiled on the fly for the first time and is cached so that the second time it is not required to compile.
MunjalD
OK. The first two questions are not for me (I didn't post that sentences).
I created a Web Site (using file system), then I created a Test Project and added a WebTest that exercise the web site.
I tried to enable Code Coverage for the Web site through Test Run configuration, selecting the Web Site folder for instrumentation (it appears in the list available for instrumentation).
But nothing happened and code coverage is not collected.
What should I do to make Web Test code coverage available?
Thanks
1) Did the test succeed?
2) Do you have code in the App_Code folder?
3) Did you look at the Run Details to see if there was any error/warning from code coverage?
4) What is the text in the code coverage results window when you expand the node? Does it say there was an error, or no data collected?
5) Can you instrument the test project as well as the web site and run the test. You should at least get data on the test project. If you get data, that shows the monitor is collecting data.
6) Are you doing anything special with the website? Are you running it as a different user or in a different context of any kind?
Chris
Gerhardo, have you solved your issues? If not, is this thread helping you? If this thread is diverging from your problem, you might want to start another thread so we can help resolve your issues directly. I have a web service dll, MyWebService.Dll, with a method Foo.. To test MyWebService.Dll, I created a standalone test project, UnitTest.dll. I added a web reference to UnitTest.dll pointing it to MyWebService.Dll. The proxies to call MyWebService.dll were created properly.
I ran the unit test and verified that it worked. Afterwards, I went to 'Test', Edit Configuration and turned on code coverage for MyWebService.Dll. I tried instrumenting it once with the 'Instrument in place' checkbox turned on and once with the 'Instrument in place' checkbox turned off.
Both times a code coverage file was created. When I analyzed it, it said that no information had been collected.
I then reset IIS and instrumented the DLL from the command line. I started coverage from the command line and then ran my unit test again. I turned code coverage off in the test configuration, as it had already been started from the command line.
The test ran fine. I stopped the code coverage collection and looked at the coverage file. The result was the same. The file was created, but no information collected. I used process explorer from sysInternals to check whether the worker process was using the instrumented DLL. It was. The DLL it had loaded was twice the size of the regular DLL.
What am I missing? The actual message is: None of the instrumented DLLs were used.
The worker process had actually created a copy of the MyWebServices.dll in the temporary asp.net folder. I checked the size of those DLLs in memory and they were the same size as instrumented web service dll. Is the symbol file supposed to located in the same place as the DLL at run-time? the pdb file was not copied over to the temporary asp.net directories.
1) Yes, The Test Succeed
2) No. I have code in the Bin folder because my site reference another project (class library,that have one class with one shared method)
3) There is no information about code coverage.
4) "Empty results generated: none of the instrumented binary was used. Look at test run details for any instrumentation problem."
5) I added the Test assembly and the class library project (referenced by my site) for instrumentation. Run the test again. It collected coverage information just for the class library (not the Test assembly or the Web Site)
6) Nothing special. From the click event of a button placed in the Default.aspx I call a shared (VB) method that is part of a class that belongs to a Class Library. Everything running with ASP.Net Developer Server (using a fixed Port).
Thanks in advance
Lucas, based on the information you've given me it sounds like your project looks something like the following:
Solution
c:\....\website Project
default.aspx
OnButtonClick calls method in code.vb
Class Library Project
code.vb
Test Project
web test
When you run the web test you aren't getting any code coverage data. This looks like a bug in our code. We aren't doing something correct with web tests. I was able to do this successfully several times, but I have to shut down the IDE after each run otherwise the test fails.
Can you create a unit test to exercise your code? That should work. You can either create a test for the class library, or you can create a test for the method that calls into it but doing this requires you to have the code under App_Code because it's part of the ASP project.
Sorry for the inconvenience. If I come up with other ideas for you I'll let you know.
Chris
Gerhardo, things work fine for me for unit tests + code coverage on an ASP.NET web service.
NOTE: If you are using a non-console Terminal Server session, code coverage may not work properly.
Here are the scenarios that worked for me:
Web Service and Test Project in same solution:
1. Create a new HTTP web service.
2. Right click a method and click Create Tests...
3. In the test project that is created, verify that unit tests run fine by opening the test view and running a unit test.
4. Open test run configuration by double-clicking the localtestrun.testrunconfig node in Solution Explorer
5. Under code coverage, check the box for the web service dll. Save and Close
6. Run the unit test again
7. Once the test results window shows that the test run is done, right click in the test results window and click Code Coverage Results
8. See the code coverage results
Web Service and Test Project in different solutions:
1. Create a new HTTP web service.
2. Open another instance of Visual Studio and create a new test project
3. Add a web reference to the web service created in step 1.
4. Change the code for TestMethod1 to test a method from the web service DLL
For example, I added a "using TestProjectX.localhost" at the top and changed the code to:
Service s = new Service();
if (!s.HelloWorld().Equals("Hello World"))
{
Assert.Fail("HelloWorld() returned an incorrect value");
}
5. Verify that the unit test works fine by going to the test view and running the unit test
6. Open test run configuration by double-clicking the localtestrun.testrunconfig node in Solution Explorer
7. Under code coverage, check the box for the test project dll. Save and Close
8. Run the unit test again
9. Once the test results window shows that the test run is done, right click in the test results window and click Code Coverage Results
10. See the code coverage results
I am not sure why it didn't work for you; one possibility is if you were using a non-console TS session. If you were not, please let me know as I will like to investigate more then.
Hope this helps. Don't hesitate to shoot more questions. Thanks for your patience.
That's right. The estructure of my Solution is the one you described and the problem is that no data coverage is collected from the ASP.Net application.
I could get code coverage from Unit Test exercising the Class LIbrary, no problem about that.
The second thing you said are the ASP.Net Unite Test, right?
We have to wait for a fix of this problem. Next CTP? :)
Thanks.
Hello MunjalD,
Unfortunately, when I attempt to follow your example in #1 above, I get the result that was originally reported.
I have a solution with a Web Service project and a test project. The unit test I am running is a data driven unit test.
I enabled Code Coverage in a new test configuration and set it as the one to be run. I ticked the checkbox for the Web Service in the list of assemblies. When I run the unit test, the test succeeds, but no code coverage is generated and I get the following error: "Empty results generated: None of the instrumented binary was used."
I see nothing from my test results that would explain this error.
What would you suggest I do next?
Regards
Edit: Note, to followup with Gerhardo's post below, I am running ASP.NET within VS, using Cassini. I haven't tried running on IIS.