Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Here</strong> is my definite guide to unit testing in <strong>Silverlight 5</strong>. This will guide to all the points you have to do, if you have no clue, where to start. This guide is about testing <strong>inside</strong> your project. You will <strong>not</strong> need a 2nd odr 3rd silverlight project to unit test. </p> <p><strong>0.)</strong> Close your Silverlight Project.</p> <p><strong>1.)</strong> Download the Silverlight 5 toolkit <a href="http://silverlight.codeplex.com/releases" rel="nofollow">here</a> and install it.</p> <p><strong>2.)</strong> Open your Windows Explorer, go to C: and then search for these two files:</p> <p><strong>Microsoft.Silverlight.Testing.dll</strong></p> <p><strong>Microsoft.VisualStudio.QualityTools.UnitTesting.Silverlight.dll</strong></p> <p>If you don't find them, something is wrong.</p> <p><strong>3.)</strong> Register these two DLLs in the GAC. For that do the follwing:</p> <p>Open the Visual Studio console as <strong>Administrator</strong>. Copy each of the statements and execute them in the console:</p> <p><em>gacutil /i "C:\Program Files\Microsoft SDKs\Silverlight\v5.0\Toolkit\dec11\Testing\Microsoft.Silverlight.Testing.dll"</em></p> <p><em>gacutil /i "C:\Program Files\Microsoft SDKs\Silverlight\v5.0\Toolkit\dec11\Testing\Microsoft.VisualStudio.QualityTools.UnitTesting.Silverlight.dll"</em></p> <p><strong>Please keep in mind, that the paths to the DLLs could be different on your machine. Just type the correct path for your machine.</strong></p> <p><strong>4.</strong> Open your Silverlight Project and add the two DLLs from above as a reference to Silverlight project. <strong>Not</strong> to your web project.</p> <p><strong>5.</strong> Add the following class to your project. It does not matter where you put it. I have an extra folder for my tests, but does'nt matter.</p> <pre><code>using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Silverlight.Testing; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Test { [TestClass] public class Test_Svc_Login { [TestMethod] public void Always_True() { Assert.IsTrue(true); } [TestMethod] public void Always_False() { Assert.IsTrue(false); } [TestMethod] public void Even_MoreAlways_False() { Assert.IsTrue(false); } } } </code></pre> <p><strong>6.</strong> Hit F6, try to compile. Everything should be ok at this point.</p> <p><strong>7.</strong> Find the file "App.xaml.cs" in your project. Inside this file you will find the following statement. Of course "new Gui.MainPage();" will be different in your project. Just search for RootVisual. </p> <pre><code> this.RootVisual = new Gui.MainPage(); </code></pre> <p>You replace this statement with the following code block.</p> <pre><code>#if DEBUG if (System.Diagnostics.Debugger.IsAttached) { //You hit F5 ONLY this.RootVisual = new Gui.MainPage(); } else { //You hit CTRL + F5 RunUnitTests(); } #else //You are in Release Mode. You hit whatever you want. this.RootVisual = new Gui.MainPage(); #endif </code></pre> <p>Now you add the following routine to your App.xaml.cs class:</p> <pre><code> private void RunUnitTests() { #if DEBUG //You hit CTRL + F5 var settings = new UnitTestSettings(); settings.TestHarness = new UnitTestHarness(); settings.StartRunImmediately = true; settings.TestAssemblies.Add(Assembly.GetExecutingAssembly()); this.RootVisual = UnitTestSystem.CreateTestPage(settings); #endif } </code></pre> <p><strong>8.</strong> Hit F6 to check if everything is ok.</p> <p><strong>9.</strong> Hit F5 to see, if you can still debug normally. <strong>Hit Ctrl+F5 to start the Unit Test.</strong></p> <p><strong>10.</strong> You are done! Now you can unit test in Silverlight 5.</p> <p><strong>Please keep in mind, that the two DLLs from the toolkit are still in "experimental" mode. There is no official support.</strong> Also keep in mind, that is approch is about running the tests inside your project. Now you could even run these test at a clients machine in release mode. If want to do that, you have to change #if DEBUG statements, but I think you see what it's all about.</p> <p><strong>Of course you can change this approach to unit test in an extra project.</strong></p> <p><strong>Happy Coding.</strong></p> <p><em><strong>HINT:</em></strong> If you have problems with System.Core.dll after registering the two DLLs, just use gacutil to register the System.Core.dll again. Like this:</p> <p>gacutil /i "C:\Program Files\Reference Assemblies\Microsoft\Framework\Silverlight\v5.0\System.Core.dll"</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload