From: Unit Test ASP.NET WebForm Unit Test ASP.NET on
Alright, so I'm trying to do the right thing and bring the concept of Unit
Testing into our ASP.NET Web Application, but we're running into walls left
and right. The biggest challenge we're facing right now involves unit
testing a .ASPX page where the assertion needs to evaluate a property of a
..ASCX user control included on this page. My thought was that we could
construct a PrivateObject the same way we do for a ASPX and be able to use
the po.GetProperty method to retrieve a property value at run-time for the
user control to test the assertion. However, we receive the
System.MissingMethodException exception when attempting to access this
property that is defined in the code-behind of the .ASCX control. Is it
possible to accomplish what I am trying or is some other method necessary?

Here is an example of the Test Method code:

<TestMethod()> _
<HostType("ASP.NET")> _
<UrlToTest("http://localhost/test/MyProspectsDetail.aspx")> _
<AspNetDevelopmentServerHost("C:\Projects\Test\Source", "/test")> _
Public Sub MyTest()
'System.Diagnostics.Debugger.Break()
Dim p As Page = TestContext.RequestedPage
Dim po As PrivateObject = New PrivateObject(p)
Dim mnu As UserControl =
p.Master.FindControl("cphSFA").FindControl("PageMenu")
Dim pp As PrivateObject = New PrivateObject(mnu)
pp.GetProperty("ItemEnabled", New Object())
'Additional code would normally follow here, but the exception is
encountered above and additional code wouldn't be reached, anyway. Also, no
assignment coded above intentionally to point out the exception, otherwise
the variable would just receive a value of "Nothing".
Assert.IsFalse(True, "Test")

End Sub

Any thoughts?

From: Gregory A. Beamer on
UI is damned near impossible to unit test. There are tools to reduce the
pain, but the fact you are mixing tags with code makes it very difficult. If
you truly want to unit test UI, try ASP.NET MVC, as it gives you better
tools. For web testing, the project type does not matter, so you can do it
on any web project. Visual Studio Team has the web testing. There are open
source web testing tools.

Another option, or an option you can also use WITH web testing, is move as
much code out of the web project as possible, so web testing becomes an
integration test and your functional code can be fully tested with unit
tests. This would be the direction I would head, as things that require
input end up testing more than the unit anyway (ie, UI testing), so the
concept of a unit test on a web UI is rather worthless. Integration and user
acceptance testing are different things.

One nice framework, for automated user acceptance testing, is the Fit
Framework. You should also look at FitNesse. They are not .NET specific, but
very useful for running a wide variety of UI cases.

Hope this helps!

--
Peace and Grace,
Greg

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

************************************************
| Think outside the box! |
************************************************

"Unit Test ASP.NET WebForm" <Unit Test ASP.NET
WebForm(a)discussions.microsoft.com> wrote in message
news:3640266D-A0D6-4E2A-994F-62E9F00FDDE7(a)microsoft.com...
> Alright, so I'm trying to do the right thing and bring the concept of Unit
> Testing into our ASP.NET Web Application, but we're running into walls
> left
> and right. The biggest challenge we're facing right now involves unit
> testing a .ASPX page where the assertion needs to evaluate a property of a
> .ASCX user control included on this page. My thought was that we could
> construct a PrivateObject the same way we do for a ASPX and be able to use
> the po.GetProperty method to retrieve a property value at run-time for the
> user control to test the assertion. However, we receive the
> System.MissingMethodException exception when attempting to access this
> property that is defined in the code-behind of the .ASCX control. Is it
> possible to accomplish what I am trying or is some other method necessary?
>
> Here is an example of the Test Method code:
>
> <TestMethod()> _
> <HostType("ASP.NET")> _
> <UrlToTest("http://localhost/test/MyProspectsDetail.aspx")> _
> <AspNetDevelopmentServerHost("C:\Projects\Test\Source", "/test")> _
> Public Sub MyTest()
> 'System.Diagnostics.Debugger.Break()
> Dim p As Page = TestContext.RequestedPage
> Dim po As PrivateObject = New PrivateObject(p)
> Dim mnu As UserControl =
> p.Master.FindControl("cphSFA").FindControl("PageMenu")
> Dim pp As PrivateObject = New PrivateObject(mnu)
> pp.GetProperty("ItemEnabled", New Object())
> 'Additional code would normally follow here, but the exception is
> encountered above and additional code wouldn't be reached, anyway. Also,
> no
> assignment coded above intentionally to point out the exception, otherwise
> the variable would just receive a value of "Nothing".
> Assert.IsFalse(True, "Test")
>
> End Sub
>
> Any thoughts?
>
From: Mr. Arnold on
Unit Test ASP.NET WebForm wrote:
> Alright, so I'm trying to do the right thing and bring the concept of Unit
> Testing into our ASP.NET Web Application, but we're running into walls left
> and right. The biggest challenge we're facing right now involves unit
> testing a .ASPX page where the assertion needs to evaluate a property of a
> .ASCX user control included on this page. My thought was that we could
> construct a PrivateObject the same way we do for a ASPX and be able to use
> the po.GetProperty method to retrieve a property value at run-time for the
> user control to test the assertion. However, we receive the
> System.MissingMethodException exception when attempting to access this
> property that is defined in the code-behind of the .ASCX control. Is it
> possible to accomplish what I am trying or is some other method necessary?
>
> Here is an example of the Test Method code:
>
> <TestMethod()> _
> <HostType("ASP.NET")> _
> <UrlToTest("http://localhost/test/MyProspectsDetail.aspx")> _
> <AspNetDevelopmentServerHost("C:\Projects\Test\Source", "/test")> _
> Public Sub MyTest()
> 'System.Diagnostics.Debugger.Break()
> Dim p As Page = TestContext.RequestedPage
> Dim po As PrivateObject = New PrivateObject(p)
> Dim mnu As UserControl =
> p.Master.FindControl("cphSFA").FindControl("PageMenu")
> Dim pp As PrivateObject = New PrivateObject(mnu)
> pp.GetProperty("ItemEnabled", New Object())
> 'Additional code would normally follow here, but the exception is
> encountered above and additional code wouldn't be reached, anyway. Also, no
> assignment coded above intentionally to point out the exception, otherwise
> the variable would just receive a value of "Nothing".
> Assert.IsFalse(True, "Test")
>
> End Sub
>
> Any thoughts?
>

You should understand what is a unit test and what is a functional test,
as I don't think you're doing a unit test.

http://www.cs.usfca.edu/~parrt/course/601/lectures/junit.html

<copied>
What is a unit test versus a functional test? A unit test is usually a
test on a method not on the overall functionality of a tool or
application. For example, testing a method that returns large prime
numbers is a unit test but testing the overall encryption algorithm that
uses that method is a functional test.
<end copy>

Here is some additional information about a when a unit test is not a
unit test, but rather it's a functional test.

http://haacked.com/archive/2008/07/22/unit-test-boundaries.aspx

In addition to this, a Web page should be as dumb as possible whereas
the Web controls are not addressed by the codebehind file directly. But
rather, the controls are passed and returned on an interface/view of a
presenter, like the presenter/view of a Model View Presenter pattern.

It's the presenter's job to get data from or set data in form controls,
which makes it much simpler to unit test, because you're testing against
interfaces and methods of the presenter.




From: Andy O'Neill on

"Unit Test ASP.NET WebForm" <Unit Test ASP.NET
WebForm(a)discussions.microsoft.com> wrote in message
news:3640266D-A0D6-4E2A-994F-62E9F00FDDE7(a)microsoft.com...
> Alright, so I'm trying to do the right thing and bring the concept of Unit
> Testing into our ASP.NET Web Application, but we're running into walls
> left
<<>>
> Any thoughts?
>
Well my first thought is about "do the right thing".
You see people were developing for years without automated testing and
managing to test their applications as they developed.
In fact we called it unit testing, but it involved the developer or another
developer.
Set that aside for a moment.
Then there is the fact that you seem to have a mature project.
Introducing unit testing into a mature project is a bad ide IMO.
You will find it is a huge overhead for little gain.
Well, unless your project is chock full of bugs and your devs prone to
changing things willy nilly.
The time to adopt unit testing is at the start of a project.
If you need it then you want to consider MVC or MVP.

From: Unit Test ASP.NET WebForm on
Mr. Arnold, I appreciate you taking the time to read and respond to my
question. However, I don't believe a lecture on unit test versus functional
test serves any benefit to the specific question asked here.

The .ASPX in this case has logic responsible for identifying if a particular
user has permission to perform a specific action. Our attempt at a unit test
was to pass in a certain condition (a specific user) then execute the private
Sub that is responsible for enabling/disabling a control (that is within the
user control) and finally evaluate that that operation enabled or disabled
the user control's menu option correctly.

In my opinion, it is very sensible to have ASPX code behind responsible for
setting properties of controls based on business logic that only exists
within that page. Given what Microsoft has already provided in the
UnitTesting framework I was asking if it was possible to leverage its members
to access UserControls within the ASPX much like we can access the members of
the page itself.

The intent was to write and evaluate a very simple test. This test only
touches one very specific function of the ASPX and in that sense I refer to
it as a "Unit Test". What this type of "Test" should actually be called is
academic in my opinion. My question was simply given the tools available is
the below scenario intended to be possible and if so what are ways to
accomplish it. From other responses I am getting the impression that this is
currently not possible or extremely over complicated outside of MVC.
Fortunately, if somebody comes to this thread in the future wanting to know
the definition of "Unit Test" versus "Functional Test" they may also be
helped. Thanks for your contribution.

Thanks,
-Rudy

"Mr. Arnold" wrote:

> Unit Test ASP.NET WebForm wrote:
> > Alright, so I'm trying to do the right thing and bring the concept of Unit
> > Testing into our ASP.NET Web Application, but we're running into walls left
> > and right. The biggest challenge we're facing right now involves unit
> > testing a .ASPX page where the assertion needs to evaluate a property of a
> > .ASCX user control included on this page. My thought was that we could
> > construct a PrivateObject the same way we do for a ASPX and be able to use
> > the po.GetProperty method to retrieve a property value at run-time for the
> > user control to test the assertion. However, we receive the
> > System.MissingMethodException exception when attempting to access this
> > property that is defined in the code-behind of the .ASCX control. Is it
> > possible to accomplish what I am trying or is some other method necessary?
> >
> > Here is an example of the Test Method code:
> >
> > <TestMethod()> _
> > <HostType("ASP.NET")> _
> > <UrlToTest("http://localhost/test/MyProspectsDetail.aspx")> _
> > <AspNetDevelopmentServerHost("C:\Projects\Test\Source", "/test")> _
> > Public Sub MyTest()
> > 'System.Diagnostics.Debugger.Break()
> > Dim p As Page = TestContext.RequestedPage
> > Dim po As PrivateObject = New PrivateObject(p)
> > Dim mnu As UserControl =
> > p.Master.FindControl("cphSFA").FindControl("PageMenu")
> > Dim pp As PrivateObject = New PrivateObject(mnu)
> > pp.GetProperty("ItemEnabled", New Object())
> > 'Additional code would normally follow here, but the exception is
> > encountered above and additional code wouldn't be reached, anyway. Also, no
> > assignment coded above intentionally to point out the exception, otherwise
> > the variable would just receive a value of "Nothing".
> > Assert.IsFalse(True, "Test")
> >
> > End Sub
> >
> > Any thoughts?
> >
>
> You should understand what is a unit test and what is a functional test,
> as I don't think you're doing a unit test.
>
> http://www.cs.usfca.edu/~parrt/course/601/lectures/junit.html
>
> <copied>
> What is a unit test versus a functional test? A unit test is usually a
> test on a method not on the overall functionality of a tool or
> application. For example, testing a method that returns large prime
> numbers is a unit test but testing the overall encryption algorithm that
> uses that method is a functional test.
> <end copy>
>
> Here is some additional information about a when a unit test is not a
> unit test, but rather it's a functional test.
>
> http://haacked.com/archive/2008/07/22/unit-test-boundaries.aspx
>
> In addition to this, a Web page should be as dumb as possible whereas
> the Web controls are not addressed by the codebehind file directly. But
> rather, the controls are passed and returned on an interface/view of a
> presenter, like the presenter/view of a Model View Presenter pattern.
>
> It's the presenter's job to get data from or set data in form controls,
> which makes it much simpler to unit test, because you're testing against
> interfaces and methods of the presenter.
>
>
>
>
> .
>