NXS-7





Quick Start

The Ideal GUI Unit Test

In an ideal world we could write a unit test like this:

    [TestMethod]
    public void TestMyWindow_in_an_ideal_world()
    {
        var w = new MyWindow();
        w.Show();
        // Verify_my_window(); 
        w.Close();
    }

But this won't work.  WPF requires an Application instance, another GUI thread and some synchronization between those threads.  More importantly, what should Verify_my_window() do?

IcuTest answers these problems with only a few extra lines of code:

    static IcuTest ICU = IcuTestStarter.IcuFromDir(@"c:\test_data");
 
    [TestMethod]
    public void TestMyWindow()
    {
        ICU.Invoke(() => {
            var w = new MyWindow();
            w.Show();
            ICU.CheckView(w, "MyWindowTest");
            w.Close();
        });
    }

This is a complete GUI test that displays, verifies, and closes a WPF Window.

  • Startup() initializes the application and the test repository.
  • Invoke() ensures that the code is executed in the right thread.
  • CheckView() is a very fast "Assert" for images.
    It uses a snapshot of the window to assert that the image is the same as the image in c:\test_data\MyWindowTest.png.

What Does CheckView() Do?

CheckView is the main testing (or Assert) mechanism in IcuTest. It performs a fast bitmap comparison between the current UI snapshot and a previously stored snapshot. Like an Assert, CheckView throws an exception when a test fails.

Tests can be executed automatically or interactively. In automatic mode, CheckView simply throws an exception when the bitmaps differ. In interactive mode, the tester is prompted to visually verify which image is correct. The check dialog looks something like this:

CheckView Window

The display lets you scrutinize the snapshot, the stored image and the difference between them. You can pass, fail or skip the test.

  • Passing saves the snapshot to the repository.
  • Failing aborts the unit test with an exception.
  • Skipping marks the test as failed/ignored, but doesn't abort the test.

IcuTests are Proper Unit Tests

Unlike typical record-and-playback or "automated" testers, IcuTest let's you directly manipulate the GUI with code. For example, what would happen if we changed MyWindow's DataContext?  Well, let's write a test:

    [TestMethod]
    public void TestMyWindow_WithDataContext()
    {
        ICU.Invoke(() => {
            var w = new MyWindow();
            w.Show();
            ICU.CheckView(w, "MyWindowTest");
 
            w.DataContext = new MyViewModel();
            ICU.CheckView(w, "MyWindowTest_with_ViewModel");
 
            w.Close();
        });
    }

With IcuTest you can now fully automate your tests; open/close windows, click buttons, select items, enter text, set properties, call methods, etc...  The possibilities are endless.

See how to make testing even easier...


IcuTest Free Download