Unit test Web Forms in Visual Studio 2012 - asp.net

I can't seem to find a walkthough/tutorial/blog posts etc on how to Unit test Web Forms in Visual Studio 2012. MSDN doesn't seem to cover it. The only thing I've been able to gleam from surfing for walkthrough's is that I need to have a clear separation of concerns to appropriately test an aspx.cs file. What am I missing? Are there any solid recent walkthrough's on how to implement simple unit testing in a Web Forms project?

Unit testing your code behind is practically impossible, since there's no context, no request and mocking all that is very difficult.
what you should do instead is separate your logic into another library and unit test that library. This would leave only view specific code in your code behind.
That code can then be tested through UI tests (with a tool like Selenium for example)

Related

automated functional web GUI testing frameworks (asp.net)

we have a mix of ASP.NET webforms and upcoming MVC projects; I am after a reliable and maintainable web GUI testing tools/framework; At the moment we have some Watin tests and seeing that Watin hasn't been actively worked on over over 15 months I am a bit reluctant to invest any more in this or should I?
What are the options and what is the best one to use.
Selenium
- Although limited Visual Studio integration seems to be a good one.
Watin
- Seems obsolete and doesn't seem to cope well with modern JQuery websites.
Telerik Test Studio
- Looks very promising, has any one tried it? is it worth it?
Are there any other viable options for ASP.NET and possible visual studio integration?
I know there is an existing question on this Web testing frameworks for ASP.NET web application but it doesn't really answer anything.
for green fields projects what are people using out there for automated function UI testing.
We are also researching ways of testing the front end of our web app.
Currently we have some tests that use the headless browser, phantomJS. The tests are written in JavaScript and look like this:
(function () {
// require is part of phantom API
var userName,
page = require('webpage').create();
page.open("http://<url of page to test>", function (status) {
if (status !== 'success') {
phantom.exit(1);
} else {
// get the text of the DOM element that displays the user name
userName = page.evaluate(function() {
return document.getElementById('userName').innerText;
});
if (userName !=== 'Guest') {
phantom.exit(1);
}
phantom.exit(); // Success!
}
});
})();
And we call this from Jenkins with a command like this (where assert_Username_EqualsGuest.js contains above JavaScript):
phantomJS.exe assert_Username_EqualsGuest.js
We also have similar tests written in powerscript, which use the System.Net.WebClient class to download dynamic resources from the web server, assert what comes back, and record the response time for keeping track of the server performance over time. Also fired by Jenkins.
So this answer is just to let you know what we are doing currently, but we are also investigating better ways. And I am very interested to hear what others are doing.
What version of Visual Studio are you using? It might be you have one of the top end ones which means you can write test scripts in CodedUI which is relatively nice and would keep the tests in a similar language/toolset that you are used to. http://msdn.microsoft.com/en-us/magazine/hh875174.aspx
If not I would vote selenium with the .net bindings, it really is the most fully featured Web automation tool and you are likely to be able to get help with it the easiest. The following web page tells you how to get selenium to work in visual studio http://www.joecolantonio.com/2012/07/31/getting-started-using-selenium-2-0-webdriver-for-ie-in-visual-studio-c/
If you want to test only in IE, you can try IBM Rational Functional Tester. It lacks support for recent Firefox version (only up to 10.6)
From the website you can download a free 30 days trial version. I'v posted about it here https://sqa.stackexchange.com/questions/4995/reviews-feedback-on-rft
Another piece of software is QA Wizard but I do not have direct experience with it.
If you are willing to pay, Telerik Test Studio is a very nice product. It's a great tool that works in different browsers. It has a very easy to use recording feature that allows you to point and click within the page and select elements to test. Another tool (paid) is Test Complete, which works very similarly. Check out the videos on both products, which the documentation is pretty good.
The thing about these tools is that they are much hands-off of requiring manual coding, but you can code tests too if the UI can't do what you are looking for.
This doesn't really answer your question directly, but you should check out the test pyramid article by Martin Fowler:
The test pyramid is a concept developed by Mike Cohn, described in his book Succeeding with Agile. Its essential point is that you should have many more low-level unit tests than high level end-to-end tests running through a GUI.
On the projects I've worked on, we've implemented unit tests using a variety of test utilities (NUnit, MBUnit, xUnit, etc), service (integration) tests using FitNesse, and left the GUI tests as a manual task (although FitNesse can get fairly close to the presentation layer if required). This has worked well for us.

Which testing frameworks to use for asp.net websites and why

I use visual studio 2005 and now my team wants me to try my hand at some testing frameworks for asp.net websites.I'm completely new to testing. I want to know what kind of automated testing can I do.Can I test my business logic,Can I test my UI.I just started using NUnit and I am getting a hang of it. For Web UI testing I've tried Nunit asp.Somewhere I found about Selenium, which of this is better.Also is there an add-in to test Javascript code with NUnit.Is there an better alternative to NUnit. I kind of find difficult to figure out why should I use this tools.
One option is to use Watin: http://watin.sourceforge.net/
There is a nice Test Recorder, which allows you to record and save your tests.
The only problem I have had with it is that it doesn't always play well with Cruise Control as it loads a browser engine in the background. If the CI server is running in an environment without the correct browsers installed and enabled, then the tests will fail. In this case we ended up using HtmlUnit, which is a java app that runs on the Cruise Control server.
http://htmlunit.sourceforge.net/

How Testing relates to ASP.NET MVC/WEBFORMS Silverlight MVVM

Ive been learning MVC2 and MVVM lately and I think it works like this.
ASP.NET MVC2 - Can test the whole web site using unit tests
ASP.NET MVC2 + jquery web service calls - Can no longer just use MSTest unit test. What is the MS product to test the javascript side?
ASP.NET Webforms - Unit Tests are near impossible when the coder doesnt create the Webforms site with Testing in mind. Therefore Asp.NET web performance tests are the closest thing to testing that is realistic. Coded UI Tests are too trivial to really be useful for things like ASP.NET validators.
ASP.NET Webforms + jquery web service calls - Can only unit test the web service calls. Cannot use Web Performance tests because of javsscript calls. Need some sort of javascript testing framework.
Silverlight - No tests. Maybe Coded UI Tests.
Silverlight MVVM - Use silverlight unit test framework to test ViewModel similiar to MVC.
Silverlight MEF - How does MEF affect testing scenerios if at all ?
Is this accurate? Is there anything I am missing ?
I am trying to make a argument to the people in charge that we should use MVC over Webforms so that we can create automated testing. As it is we are doing Webforms all in one project and impossible to test so people just test off of manual scripts :(
Rather than specifying the reasons why you should use MVC over Webforms, I'd take a step back and sell the management team on why you should use unit tests. After that's sold you have a case of saying that MVC would allow you to do this more efficiently than webforms.
Are you looking at going the full TDD route, or just creating tests after? I'd highly recommend going down the TDD path even though it does have a steeper learning curve and will lessen your productivity while you are learning it.
Since you are already looking into testing, you probably know most of these, but I'll re-iterate some of the benefits:
Less defects get through to QA.
Tests can be created for issues that QA and customers find.
Designing for testing creates a more loosely coupled application which tend to be more maintainable.
Increased developer confidence when making changes leading to sustained productivity as the application code base matures.
Tests are great documentation for new developers joining the project, as they can see what's trying to be achieved.
Note: the cost of fixing a defect that has made it through to production can be up to 80 x the cost of finding and fixing it in the development and QA process (I'll try to find my source for that figure).
Unit testing is only one piece of the puzzle though, you'll also want to look at using a Continuous Integration server such as CruiseControl.NET to automate your builds and tests. This will make sure that everyone keeps the build in a working state.
For an existing Webforms project you might also want to look into the Web Client Software Factory. I've found it to be very useful for migrating legacy webforms apps over time, though it's a little more convoluted than MVC2.
I don't know about other technologies, but I can talk about using Silverlight with MVVM pattern. Using MVVM pattern along with command pattern, we have been able to unit test all of our C# code. I mean barring xaml code, we have a very high code coverage for our C# code. We actually use a modified version of MVVM wherein we also use a controller. If you ensure that you have no code in code-behind files, you can simulate button click/other such UI events. This is very useful for integration tests. We have found out that our pattern of MVVM along with controller and using command pattern facilitates unit testing.

how to exclude a Web Reference from Code Coverage in VS 2008 Team System

When I run my MSTest tests in Visual Studio 2008 Team System and get code coverage results, I always see a particular web service included. I don't care how well this web service is tested, I'm intentionally only using a small part of it. How can I exclude the Web Reference from showing up in my Code Coverage results?
I see that someone asked this very question over on Microsoft Connect and it's marked as postponed, but I was hoping someone knew of a workaround.
A work-around would be to put the web service in a separate assembly and not run code coverage on that assembly/project.
I do not believe this is possible in the current version of the code coverage feature. It will report the code coverage results for the entire assembly.
One thing you could try is to tag the Web Reference with the DebuggerNonUserCode attribute. This is used by several tools to filter out code that is not actually owned by the user. I do not know if the code coverage tool filters this attribute but it's possible that it does.

How apply Unit tests in ASP.NET webforms

I'm developing a website in asp.net webforms with 3 layers; UI, BLL and DAL
The website is already developed, but i like have more control about the unit tests of each form
Pass specific values at specific inputs for i see, if application survives or not.
I already study about NUnit but in webforms in UI layer how can apply these tests?
What i wnat is get some way to test UI (validations) without have to access to the BLL as i was an user.
I'm trying to add the Unit tests to my app but i not sure how to do it!
somebody can help my small-bigger problem?
You can add unit test to your UI using one of these tools
http://watin.sourceforge.net/
http://seleniumhq.org/
With both tools you can generate C# or VB classes that can be used by NUnit.
IMHO, UI test are not as reliable as the common unit tests. Bacause timing conditions you may randomly find that some test will fail.
I used both tools and I recommend selenium mainly because you can generate test that can be run against any browser.
Here an example how to use Watin with NUnit
http://www.codeproject.com/KB/applications/SimpleUITestHarness.aspx
And here a guide how to use Seleniun with Nunit
http://www.lostechies.com/blogs/agilecruz/archive/2009/02/10/how-to-configure-selenium-rc-for-use-in-c-nunit-tests.aspx
If I understand your question correctly, you're asking how to automate testing a WebForms UI. If I misunderstand, please correct this understanding.
Frankly, I don't think there is a good answer out there for this. There are two options, however, that work okay:
Visual Studio Test Edition
Telerik's Web Testing (both a free version and a version with a cost)

Resources