c# webforms testing strategy - c#

I wondered if anyone had links or comments on strategy as regards to testing a webforms application, I am referring specifically to automated testing practices, and unit testing.
My current strategy has been to separate business logic into a separate DLL that can be Unit tested on a class by class basis. However when it comes to testing the web Forms themselves using code UI tests, it appears I should be running two instances of visual studio 2010, one for the application and the other in order to run the created UI tests.
I found this link:
Testing ASP.NET webforms applications
However while this link was interesting it does not answer my question with regard to a strategy on a green fields project.
Although MVC3 would solve some of this, part of my reason for sticking with webforms is that I am slowly migrating aspects of a legacy system and to start from scratch on everything, when I can migrate strategic parts does not make sense.
Any links comments or observations would be welcome. Thanks.

1 You can try with Selenium if you want just test your interfaces
http://seleniumhq.org/
2 On functionnal subject (Code behind) It's very difficult to test because you must mock your pipeline, when i compare to MVC.
So for example you can mock HttpContextBase
var mock = new Mock<HttpContextBase>();

Related

What is the proper way to unit test .NET Core Web Application?

I'm new at web development (used to develop desktop apps). I didn't manage to find a proper way to unit test internals of my web application.
Let's say I do have a Foo class which do have a strictly defined behaviour (completely independed from DB and/or UI). I need to make sure that my implementation is corrent. In the desktop app I just add another test dll with unit tests (if the class is public) or just internal class with tests. And start it with nunit to make sure that it works. But how is should be done in the web application?
The only way I can think of is to add some debug-only pages on which I will run my test and ignore them during the deploy. But I think there should be some unit test framework for that and I just didn't manage to find it.
Do the .net core web developers have any standard way to do it (something like nunit)?
Yes, the recommended unit testing framework to use with .NET Core apps is called Xunit. You can read an overview and walkthrough here.

Retrofit unit tests to large solution, IOC, Moq

I am in the process of retrofitting unit tests for a asp.net solution written in VB.Net and c#.
The unit tests need to verify the current functionality and act as a check for future breaking changes.
The solution comprises of:
1 MVC web project
written in vb.net (don't ask, it's a legacy thing)
10 other supporting projects each containing logically grouped functionality
written in C#, each project contains repositories and DAL
All the classes are tightly coupled as there is no inversion of control (IOC) implemented anywhere, yet.
currently to test a controller there is the following stack:
controller
repository
dal
logging
First question, to unit test this correctly would I setup 1 test project and run all tests from it, or should I setup 1 test project for each project to test the functionality of that DLL only?
Second question, do I need to implement IOC to be able to use MOQ?
Third question, is it even possible to refactor IOC into a huge solution like this?
Forth question, what other options are available to get this done asap?
I am in the process of retrofitting unit tests for a asp.net solution written in VB.Net and c#. The unit tests need to verify the current functionality and act as a check for future breaking changes.
When working with a large code base that doesn't have unit tests and hasn't been written with testing in mind, there is a good chance that in order to write a useful set of unit tests you will have to modify the code, hence you're going to be triggering the event that you're planning on writing the unit tests to support. This is obviously risky, but may not be any riskier than what you're already doing on a day to day basis.
There are a number of approaches that you could take (and there's a good chance that this question will be closed as too broad). One approach is to create a good set of integration tests ensure that the core functionality is working. These tests won't be as fast to run as unit tests, but they will be further decoupled from the legacy code base. This will give you a good safety net for any changes that you need to make as part of introducing unit testing.
If you have an appropriate version of visual studio, then you may also be able to use shims (or if you have funds, typemock may be an option) to isolate elements of your application when writing your initial tests. So, you could for example create shims of your dal to isolate the rest of your code from the db.
First question, to unit test this correctly would i setup 1 test project and run all tests from it, or should i setup 1 test project for each project to test the functionality of that dll only?
Personally, I prefer think of each assembly as a testable unit, so I tend to create at least one test project for each assembly containing production code. Whether or not that makes sense though, depends a bit on what's contained in each of the assemblies... I'd also tend to have at least one test project for integration tests of the top level project.
Second question, do i need to implement IOC to be able to use MOQ?
The short answer is no, but it depends what your classes do. If you want to test using Moq, then it's certainly easier to do so if your classes support dependency injection, although you don't need to use an IOC container to achieve this. Hand rolled injection either through constructors like below, or through properties can form a bridge to allow testing stubs to be injected.
public SomeConstructor(ISomeDependency someDependency = null) {
if(null == someDependency) {
someDependency = new SomeDependency();
}
_someDependency = someDependency;
}
Third question, is it even possible refactor IOC into a huge solution like this?
Yes it's possible. The bigger question is it worth it? You appear to be suggesting a big bang approach to the migration. If you have a team of developers that don't have much experience in this area, this seems awfully risky. A safer approach might be to target a specific area of the application and migrate that section. If your assemblies are discrete then they should form fairly easy split points in your application. Learn what works and what doesn't, along with what benefits and unexpected pain you're feeling. Use that to inform your decision about how and when to migrate the rest of the code.
Forth question, what other options are available to get this done asap?
As I've said above, I'm not sure that ASAP is really the right approach to take. Working towards unit-testing can be done as a slow migration, adding tests as you actually change the code due to business requirements. This helps to ensure that testers are also allocated to catch any errors that you introduce as part of the refactoring that might need to take place to support the testing.

Unit Testing for Winforms using MVC pattern?

There is need to create small scale winform applications for private use by my company for interaction with our database. Period. I know that .NET MVC has a very mature unit testing framework for the MVC pattern as I want to do TDD.
Thus, is it possible to use NUnit or some other easy/mature unit testing framework (EDIT: Based on my experience with ASP.NET) with the following tutorial given here? I have googled and checked my technical book library, and there is a distinct lack of documentation for how to do effective unit testing for winforms. Thus, I am turning to this forum hoping individuals can share their domain knowledge with me.
The most I have found has recommended the MVC pattern (which I agree with), but not how to address specific issues with the winform. For example, how do I test a button click and subsequent action by that button?
I plan on using C# VS13 .NET 4.5. I am excited to join this resource and will contribute rep to all who answer my inquiry. Thanks.
As you have probably noticed, the idea there is to have your view described with an interface.
Thus, the controller doesn't really need a window, it needs a class implementing the interface.
And thus, you could have yet another, auto-mocked or manual implementation of the view interface that doesn't involve the WinForms subsystem but rather, exposes the data to write your assertios.
Having your stub view, you just write a script against it. You expose some methods from the class that allow you to automate the interaction:
public class ViewStub : IView
{
// implement the view interface as it is but also
// an extra stuff to let you automate this in your unit tests
public void RaiseButtonClick()
{
this.controller.DoTheButtonClickStuff();
}
}
Then your test becomes (I follow the convention from the tutorial)
ViewStub form = new ViewStub();
IModel mdl = new IncModel();
IController cnt = new IncController(view,mdl);
form.RaiseButtonClick();
Unit Testing a GUI is something that is independent of the GUI library used. The answer to the general case answers your case as well:
How can I unit test a GUI?
Unit testing a GUI is done by minimizing the footprint of classes that do depend on your GUI framework. Then the classes that still depend on the GUI framework are not unit tested (which is not at all the same as "are not tested"). Those classes are the "View" in patterns like MVP, MVC, MVVM.
Conclusion: Every good .Net Unit Testing framework is a good WinForms Unit Testing framework. One example of such a framework is NUnit, which you mentioned in your question.
Unit testing is not inteded for UI. Of course you can do it, but I don' recommend it for few reasons: You cannot unit test DPI changes, multiple screen, how your program acts in different window states or what happens if user moves with tabulator between your controls. Unit testing UI will only give you false safety.
Use UI automation tools to test UI automatically if you want to, but leave the unit testing out of it. You can keep the UI layer thin as possible and this is also a good design practice. You can use unit testing for other classes which are used in your winforms app.

how to perform Nunit after asp.net web application is created

I have created one web application which includes login page and several other pages which include input controls.How i can perform Nunit testing on this pages.
It will be very helpful if you provide guideline for just only login page.
Login page is created using Login.aspx and Login.aspx.cs file.
It is not created using MVC.
You are confusing unit testing with automated UI testing. NUnit, as its name states is for creating automated tests for separated pieces of code. With this tests you check if concrete piece of code does what it should do. To check this you substitute all its dependencies (all classes/interfaces it interacts with) with stubs/mocks (more) which place the role of placeholders for real functionality. Thanks to this you can check if tested class behaves the way you expect it to behave because you have full control over its dependencies, so the only "unknown behavior" comes from object under test.
If you, however would like to automatize tests of user interface you can use special environments dedicated for this task. You can easily find dozens of frameworks for this, one of which is FitNess

How test Model Logic class with Unit Test in asp.net mvc4 without run webserver?

I have logic class: MyUtil.cs inside Models folder for asp.net mvc4 app.
Auto generated unit tests for MyUtils.cs create unit test that working with webserver (use attr:[HostType("ASP.NET")][UrlToTest("http://localhost:1139/") etc..]).
What should I change for make this unit test run without server?
Thanks,
Yosef
The best and simplest solution would be to not use the auto-generator for unit tests but write the tests yourself. There are many articles and tutorials out there and this one can be a good place to start:
Introduction to Test-Driven Development with ASP.NET MVC

Categories