Trying to nunit test a form

D

David Thielen

Hi;

I have some simple nunit tests for my forms. When I run nunit from Visual
Studio they run fine. But when I use the nunit command line app to tun it I
get the following:
21) net.windward.autotag.controls.TestTagEditor.TestSet :
System.Threading.ThreadStateException : ActiveX control
'8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the
current thread is not in a single-threaded apartment.
at System.Windows.Forms.WebBrowserBase..ctor(String clsidString)
at System.Windows.Forms.WebBrowser..ctor()
at net.windward.autotag.controls.ImportControl.InitializeComponent() in
C:\src\kahuna\AutoTag\AutoTagCore\net\windward\autotag\controls\ImportControl.Designer.cs:line 34
at net.windward.autotag.controls.ImportControl..ctor() in
C:\src\kahuna\AutoTag\AutoTagCore\net\windward\autotag\controls\ImportControl.cs:line 23
at net.windward.autotag.controls.TagEditor.InitializeComponent() in
C:\src\kahuna\AutoTag\AutoTagCore\net\windward\autotag\controls\TagEditor.Designer.cs:line 557
at net.windward.autotag.controls.TagEditor..ctor(IFramework app,
TagPosition tagPos) in
C:\src\kahuna\AutoTag\AutoTagCore\net\windward\autotag\controls\TagEditor.cs:line 134
at net.windward.autotag.controls.TestTagEditor.TestSet() in
C:\src\kahuna\AutoTag\TestCore\net\windward\autotag\controls\TestTagEditor.cs:line 266

What's going on and what do I need to do to let these tests run from the
command line?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
L

Linda Liu[MSFT]

Hi Dave,

Could you tell me what command line you are using to run your unit test?

From the exception stack you showed, it seems that you use a WebBrowser on
the form to be tested.

I create a Windows Application project named 'WindowsApplicationForTest'
and add a WebBrowser control on the form. I add a public method called
'Navigate' in the form to navigate a URL using the WebBrowser. Then I
create a unit test for the public method 'Navigate'.

The following is the sample code in my test.

// Windows Application project

public partial class Form1 : Form
{
...
public bool Navigate()
{
this.webBrowser1.Navigate("http://www.google.com");
return true;
}
}

// Test project
<TestClass()> _
Public Class Form1Test
...
<TestMethod()> _
Public Sub NavigateTest()
Dim target As Form1 = New Form1

Dim expected As Boolean
Dim actual As Boolean

expected = True
actual = target.Navigate

Assert.AreEqual(expected, actual,
"WindowsApplicationForTest.Form1.Navigate did not return the expected
value.")

End Sub
End Class

Then I double click the 'WindowsApplicationForTest.vsmdi' under the
'Solution Item' node in the Solution Explorer and create a new test list
named 'TestList1' under the 'Lists for Tests' node. Select the 'All Loaded
Tests' node and drag the 'NavigateTest' on the right panel onto the newly
created Test List 'TestList1'.

From the VS2005 Command Prompt, swith to the Solution folder and type the
following command:

MSTest /testmetadata:TestProject1.vsmdi /testlist:TestList1

The result is that the unit test is run properly without any exception.

If there's any difference between your test and mine, please let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

David Thielen

I am using nunit-console.exe in NUnit 2.2.7. My code is:

[TestFixture]
public class TestTagEditor
{

....

[Test]
public void TestDefaultReturn()
{

// new tag
using (TagEditor dlg = new TagEditor(CreateFramework(), new
TagPosition(null, new BaseTag[0], null)))
{
Assert.IsNotNull(dlg);
Assert.AreEqual("<wr:blush:ut/>", dlg.TagPosReturn.Tag.toText());
}

// existing tag
using (TagEditor dlg = new TagEditor(CreateFramework(), new
TagPosition(BaseTag.factory("<wr:forEach var=\"dave\" select=\"/root\">", 0,
false), new BaseTag[0], null)))
{
Assert.IsNotNull(dlg);
Assert.AreEqual("<wr:forEach select=\"/root\" var=\"dave\">",
dlg.TagPosReturn.Tag.toText());
}
}
}

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
L

Linda Liu[MSFT]

Hi Dave,

Thank you for your reply!

I do more research on the NUnit and find that you should use the
configuration file to ensure that NUnit uses STA appartment state to run
your tests.

For more information on how to do this, please refer to the following
article:

http://watin.sourceforge.net/apartmentstateinfo.html#nunit

Hope this helps.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
P

Phil

This is a great thread. I want to add some complexity to this test to
demonstrate the difficulty I am having.

Your test is challenging a boolean but has not actually challenged the value
in the browser controls URL.

In my unit tests, run as single apartment thread (this article helped me get
going,) I have not been able to retrieve this value even after setting it. I
believe that is because the control is unable to resolve the URL and remains
null.

So I have been able to right Unit tests all the business logic that
determins what my Url will be. I can even test the field of the Url that
will be set but I can't test that the Url has been set. I am looking for
suggestions.

I created an App project and a unit test project.
Here is the form I am testing from the app.

public partial class TestBrowserForm : Form
{
public TestBrowserForm()
{
InitializeComponent();
}

public void button2_Click(object sender, EventArgs e)
{
this.webBrowser1.Navigate("www.yahoo.com");
}

public void Google_Click(object sender, EventArgs e)
{
this.webBrowser1.Navigate("www.google.com");
}

public void Nasa_Click(object sender, EventArgs e)
{
this.webBrowser1.Navigate("www.nasa.gov");
}
}

Here is the unit test

[TestFixture]
public class BrowswerTest
{
[Test]
public void Navigate()
{
TestBrowserForm browser = new TestBrowserForm();
Assert.IsNotNull(browser.webBrowser1);
Assert.IsNull(browser.webBrowser1.Url);
browser.Google_Click(this, null);
Assert.IsNotNull(browser.webBrowser1.Url);
}
}

I have no chance of testing what the Url is because it is always null. Do
you have any suggestions on how to run a unit test that will set the Url in a
way that we can challenge that the value has actually been set?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top