How Do I Unit Test Silverlight 3 in VS2010? - silverlight

I am currently working on a project that uses the Silverlight 3 SDK and I want to create unit tests for my Silverlight code. I want these tests to not have to run in a browser context. I have referenced the Silverlight Unit Testing binaries that come in the SDK (Microsoft.Silverlight.Testing and Microsoft.VisualStudio.QualityTools.UnitTesting.Silverlight) which gives me the framework necessary to write the tests (the test attributes are present).
The issue I am currently running into is that the unit tests in Silverlight are not being recognized by any test runners. Test-Driven.NET will only run the most basic of unit tests (i.e. no TestInitialize method) and Visual Studio 2010's test runner does not perceive any tests to run at all. For example, even this simple unit test will not run:
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace SlUnitTests
{
[TestClass]
public class Class1
{
[TestMethod]
public void Test1()
{
Assert.Fail();
}
}
}
Any ideas?
Edit:
I forgot to mention that this project was originally a Visual Studio 2008 project. I had these unit tests for Silverlight in VS2008 using the same Silverlight 3 SDK and they ran just fine. Both the ReSharper test runner and MSTest on the command line were able to run the Silverlight-based unit tests - no browser context was required. The Silverlight unit tests broke when my project was converted to VS2010.
Sorry for the confusion.

Unfortunately there's no easy integration into the standard (non-browser) interface using these assemblies.
One option if you need to exercise basic code (no user interface code; nothing that uses System.Windows), you could create a proper class library and/or test project for full .NET in Visual Studio, and then "link" in the source files from your Silverlight project.

Related

Debugging xUnit tests in Visual C# Express?

I have written xUnit tests for my C# solution.
Visual C# Express does not integrate with unit test tools, so people usually use external tools, and indeed I have used the external xUnit test runner so far.
But now I would like to debug a particular test within Visual C# Express, because this test crashes the xUnit test runner.
I am thinking of creating a dummy executable project that just references the unit tests project and calls the test.
Any smarter idea?
If you're willing to upgrade to Visual Studio 2012 Express for Windows Desktop, you can use the xUnit.net runner for Visual Studio 2012. Otherwise you might be able to attach to the test runner's process (Debug -> Attach to process...) and debug it that way.
I'm not sure if this functionality exists in the Express version, but you should be able to do Tools -> Attach To Process, then attach to the xunit test runner application (make sure it's running). Place a breakpoint in one of your tests, and when you Run the test(s) in the xUnit Test Runner, you should be able to debug like normal.

Unit Testing in Lightswitch

I'm thinking of running unit tests for the business logic in the Lightswitch application. I've added a Silverlight unit test project (from the Silverlight Toolkit), however I couldn't retrieve the code from Lightswitch to write tests on.
Or is there really no way and the best way is to move the code out to another Silverlight class library?
Ended up creating a separate test project, then adding the code to be tested through the "Add as Link" option. This way I was able to unit test the code even if the logic is inside the Lightswitch project.
You can test custom LightSwitch code by using the GoF bridge pattern, see https://github.com/legg/UnitTestingLightSwitch2011

Moving NUnit/ReSharper tests to Silverlight

We have a WinForms application that we're planning to port to Silverlight. Obviously the UI will need to be totally rewritten, but we have a lot of business logic that's well-tested with NUnit tests. We use ReSharper to run the tests inside the IDE, and nunit-console to run the tests on the continuous integration machines.
As we start moving our business-logic classes into Silverlight assemblies, how do we get these unit tests to run against those Silverlight assemblies? My understanding is that NUnit can't run Silverlight tests without significant tweaking, and said tweaking (in the form of a project template) appears to be VS2008- and Silverlight 3-specific (we'll most likely be using VS2010 and Silverlight 4 for our port).
We can move our tests to another testing framework if that's the best option, but there doesn't seem to be much else besides NUnit. The Silverlight Unit Test Framework looks like it runs inside the browser, which is a non-starter for continuous integration.
I'm aware that Silverlight 4.0 assemblies can be loaded into a .NET 4.0 executable, but I'm unsure of what this means for unit testing. Would it work to compile our business-logic assembly as a Silverlight 4.0 DLL, and write a full-framework .NET 4.0 NUnit test assembly that references the Silverlight DLL? It seems like this might work, but has anyone done this successfully with unit tests?
Bottom line, what should we do for our unit tests? We need a solution that
still lets us run tests in the ReSharper test runner;
can also be run from the command line;
works in VS2010.
We don't instantiate any UI objects in our tests, so those pesky DependencyObject threading issues aren't a concern. We just need to test our Silverlight business logic.
Not sure if NUnit is supported or how difficult it would be to add, but you might check out http://AGunit.codeplex.com for ReSharper Silverlight support.
I wrote a little tool to C.I. silverlight tests and attempt to help in normal dev/tdd scenarios - you can go check out http://StatLight.codeplex.com.
You should be able to find an NUnit build for silverlight out there (there's a few). Not sure of any runners that work with NUnit in the browser. However StatLight can run some basic NUnit functionality for you.
Hope this gives you a little help.

Silverlight Unit Testing Framework running tests in external class library

I'm currently looking into different options for unit testing Silverlight applications. One of the frameworks available is the Silverlight Unit Test Framework from Microsoft (developed primary by Jeff Wilcox, http://www.jeff.wilcox.name/2010/05/sl3-utf-bits/).
One of the scenarios I'm looking into is running the same tests on both Silverlight 3 (PC) and Windows Phone 7. The Silverlight Unit Test Framework (SLUT) runs on both PC and phone. To prevent having to copy or link files I would like to put my tests into a shared test library, that can be loaded by either a WP7 application using the SLUT, or a Silverlight 3 application using SLUT.
So my question is: will SLUT load unit tests defined in a referenced class library, or only in the executing assembly?
I did some research, and turns out you can tell the SLUT test runner which assemblies to test. You do this by configuring the test settings when creating the test page.
private void Application_Startup(object sender, StartupEventArgs e)
{
var setting = UnitTestSystem.CreateDefaultSettings();
setting.TestAssemblies.Add(typeof(TestInReferencedAssembly).Assembly);
RootVisual = UnitTestSystem.CreateTestPage(setting);
}
In the code example above the TestInReferencedAssembly is a test defined in a class library referenced by the application running the SLUT test runner. You can add the assemblies the test runner should scan to find unit tests. In this case I add the assembly the TestInReferencedAssembly belongs to.
I have not tested if this functionality works when executing the SLUT on Windows Phone 7, but if it does we should be able to run the same test suite on Phone and Desktop.

Resharper support for Silverlight Unit Test Application

HI All
Is there a version of Reshaper that can be used to run Silverlight unit test. I am using Resharper 4.5 although it shows test icons against test methods in the class, but it does actually run the test.
Thanks
I'm the author of the AgUnit plugin.
Silverlight 3 support is currently working if you build the plugin from source, a new release is coming in the next couple of weeks.
If you want to run the tests on a build server, you should take a look at StatLight: http://statlight.codeplex.com/ You should be able to set it up easily with TeamCity.
Update: Silverlight 3 support should be working in the new 0.2 release of AgUnit.
I doubt it, although I am happy to be proven wrong. The test runner for MS Silverlight Unit Test suite is actually a Silverlight app, that runs in the browser. This is done in order to simulate the Silverlight runtime environment, which is different from desktop runtime.
Try this:
http://agunit.codeplex.com/

Resources