Archive | Unit test RSS feed for this section

Unit Testing and Test Automation in VS2012 Part 2: Integrating Selenium with Visual Studio

4 Jul

In the first part of this series we discussed about integrating NUnit with Visual Studio 2012. One of NUnit’s strong points is its extensibility which has been used to expand unit testing even further — as far up as the user interface in fact. This is where Selenium comes in.

Selenium runs on top of NUnit so that it can run tests against instances of web browsers. With the new testing features of Visual Studio 2012 we can now use it to test web applications.

If you haven’t read the first part of this series and haven’t followed the steps given there, I suggest reading it now and following the steps to install NUnit on Visual Studio 2012 first. They are a prerequisite to the next steps in this article.

Integating Selenium into Visual Studio 2012

Since we’ve already integrated NUnit to our Visual Studio solution, why not go all the way and use Selenium as well? Selenium uses the NUnit Framework for its tests anyway — it’s just a matter of adding several more components to allow Selenium’s Webdriver to fire up a web browser and start executing tests.

To do this we need to extend our project a bit:

  1. Open up the Manage NuGet Packages window again (right-click on References under the project -> Manage NuGet Packages) and in the search box type “Selenium”
  2. Select “Selenium Webdriver” and click Install to add Selenium references to your project.
  3. Select “Selenium Webdriver Support Classes” and click Install — these are some additional references necessary to run Selenium tests in your Visual Studio solution.
  4. Download IE Webdriver from the Selenium download page (choose the appropriate 32 or 64 bit version) and unzip.
  5. Righ-click on the project name and click “Add existing item…”
  6. Browse to the folder containing IEDriverServer.exe and choose that file to add to the project
  7. Under the project tree right-click on the file and click Properties
  8. Set the value of the field “Copy to Output Directory” to “Copy if newer”

The steps regarding the web driver ensure that the exe required to open up a browser is always copied to the /bin/Debug folder, from where it will in turn be used to call on the browser executables and open up the browser.

We can use the project we already set up above to test this. Add a new class and set it to have the following code. Note the additional OpenQA using declarations, aside from the NUnit.Framework that we’ve used before:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
namespace TestAutomation
{
    [TestFixture]
    public class Driver
    {
        IWebDriver driver;
        [SetUp]
        public void Setup()
        {
            // Create a new instance of the Firefox driver
            driver = new InternetExplorerDriver();
        }
        [TearDown]
        public void Teardown()
        {
            driver.Quit();
        }
        [Test]
        public void GoogleSearch()
        {
            //Navigate to the site
            driver.Navigate().GoToUrl("http://www.google.com");
            // Find the text input element by its name
            IWebElement query = driver.FindElement(By.Name("q"));
 
            // Enter something to search for
            query.SendKeys("Selenium");
 
            // Now submit the form
            query.Submit();
 
            // Google's search is rendered dynamically with JavaScript.
            // Wait for the page to load, timeout after 5 seconds
            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
            wait.Until((d) => { return d.Title.StartsWith("selenium"); });
 
            //Check that the Title is what we are expecting
            Assert.AreEqual("selenium - Google Search", driver.Title);
        }
    }
}

Run the tests again. The test should open Internet Explorer and then open up Google. The result in the Test Explorer would look like this, just like our NUnit test:

Conclusion

With the capability to tightly integrate NUnit and Selenium in Visual Studio 2012 solutions and projects, Microsoft redeems itself by rectifying the ghosts of MSTest: it brings unit testing much closer to coding, it finally allows test-first/test driven development, and it opens up the capability for third-party unit testing frameworks to run in Visual Studio as first class citizens. This allows other open source testing frameworks like xUnit.net, QUnit/Jasmine, and MbUnit to run seamlessly with Visual Studio 2012.

References

I’d like to point out Anoop Shetty’s blog post on Selenium integration from which I took the steps and code for the Selenium test used in this post. While his post was applicable to Visual Studio 2010 the steps were practically the same for Visual Studio 2012.

Unit Testing and Test Automation in VS2012 Part 1: Running NUnit natively in Visual Studio

4 Jul

One of the most criticized features of Visual Studio ever since it came out has been the ability, or lack and inadequacy thereof, of the IDE to integrate third-party unit testing libraries into it. MSTest was not only too late but was also too lame — it wanted users to rely on automatically generated tests put up after code has already been written. This runs so counter to the tenets of Test Driven Development that MSTest was simply ignored, and many developers came up with ways to hack third party unit-testing frameworks like NUnit, MBUnit, xUnit and so on for their .NET projects. Inevitably, Microsoft and MSTest ended up being vilified by the believers of TDD.

With Visual Studio 2012 Microsoft provided a means of using frameworks like NUnit and Selenium to actually be run within its IDE, in a tightly integrated way, and in turn, the ability to code test-first.

Let’s talk about how to make that happen.

Setting up the NUnit Test Adapter

First in our agenda is to wire Visual Studio 2012’s testing framework to NUnit. To be able to do this we need to download and install the NUnit Test Adapter extension, which we will do via Visual Studio’s “Extension Managers” feature:

  1. With Visual Studio 2012 started, go to the Tools Menu and click on Extensions and Updates
  2. On the left tab click on “Online” and on the Search field on the upper right type in “NUnit Test Adapter”. You will need an internet connection for this to work.
  3. When the NUnit Test Adapter item appears click the “Download” button. Once the download is complete follow the instructions to install the NUnit Test Adapter
  4. At the bottom of the window you may be asked to click “Restart Now” to allow the newly installed components to take effect within Visual Studio 2012.

Once you’ve finished this Visual Studio 2012 is now ready to run NUnit tests within the IDE.

Creating your first NUnit test project

Let’s now create our first NUnit test project.

  1. Go to File -> New -> Project and under Visual C# choose “Class Library” and rename to “NUnitTestDemo”. You may also want to rename your “Class1.cs” into something more sensible.
  2. In the Solution Explorer right-click on references then click “Manage NuGet Packages”.
  3. On the left tab click on “Online” and then on the Search field type “NUnit” this time. When the result appears click “Install”.

All the relevant classes required for NUnit should now be included in the project — it’s now time to write some unit tests!

Our bare code so far looks like this (I’ve renamed the project “NunitTestDemo” and the default class “FirstUnitTest”):

1
2
3
4
5
6
7
8
9
10
11
12
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NunitTestDemo
{
    public class FirstUnitTest
    {
    }
}

To check if NUnit works on Visual Studio let’s try adding bits of code and one dummy test:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
namespace NunitTestDemo
{
    [TestFixture]
    public class FirstUnitTest
    {
        [Test]
        public void TrueTest()
        {
            Assert.IsTrue(true);
        }
    }
}

To run this test, click on the Test menu, Run, then All Tests. You could also use the shortcut Ctrl-R, then A.

The test result under the new “Test Explorer” tab will look like this:

Details like the name of the tests and execution times of both individual and all tests appear in this tab.

Let’s add a failing test just to see what it looks like:

1
2
3
4
5
[Test]
public void MathTest()
{
    Assert.AreEqual(0, 1 + 1);
}

Running the tests again, the result would look like this:

Additional details come up — highlighting the failed test reveals the expectations of the failing test, the actual result, and even the stack trace, crucial for figuring out why the test is failing.

You’re now ready to use NUnit to code test-first for your classes.

But you could also use it for your front-end code, particularly web applications. On the next installment of this 2-part series I’ll show you how to use Selenium for front-end testing in Visual Studio.

References

Peter Provosts’s presentation on Visual Studio 2012 unit testing features in TechEd Europe was a crucial reference for this post.

Efficient JavaScript unit testing (Video + Slide)

2 Jun