Udostępnij za pośrednictwem


Walkthrough: Creating and Running a Smart Device Unit Test

This walkthrough explains how to create, run, and customize a series of tests by using the Team System testing tools for device projects. You start with a C# smart device application that is under development. You then create a test project, create a unit test, run the test, and examine the results.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

In this walkthrough, you will complete the following tasks:

  • Open an existing project and create a method.

  • Create a unit test and device test project.

  • View run configuration.

  • Run a unit test.

  • Run a unit test from a command prompt.

Prerequisites

You must complete the following procedures, Walkthrough: Creating Windows Forms Applications for a Device, before you can begin this walkthrough. The prerequisite walkthrough installs .NET Compact Framework 2.0 on the Pocket PC 2003 Emulator which is required to complete this walkthrough. Either leave the emulator window open or save the emulator's state. For more information, see Saved-State Files.

Open an Existing Project and Create a Method

To open the existing project and create a method

  1. Start Visual Studio.

  2. On the File menu, point to Open, and then click Project/Solution.

    The Open Project dialog box appears.

  3. Navigate to the DeviceSample project that you created in the prerequisite walkthrough and then click OK.

    The DeviceSample project is displayed in Solution Explorer.

  4. Right-click Form1.cs (C#) or Form1.vb (Visual Basic) in Solution Explorer and then click View Code to open the file in the Code Editor.

  5. Position the cursor inside the Form1 class.

  6. Insert the following code:

    public int Add(int a, int b) {return a + b;}
    
    Public Function Add(ByVal a As Integer, ByVal b As Integer) As Integer 
        Return a + b
    End Function 'Add
    
  7. On the Build menu, click Build Solution.

    You now have a project named DeviceSample that contains source code to test. The namespace for DeviceSample, DeviceSample, contains the public class Form1, whose method you will test in the following procedures.

Create a Unit Test and Device Test Project

To create a unit test and device test project

  1. Right-click Form1.cs (C#) or Form1.vb (Visual Basic) in Solution Explorer and then click View Code to open the file in the Code Editor.

    Note

    The DeviceSample project contains the code that you want to test, also referred to as the code under test.

  2. In the Form1 class, scroll to the Add method.

  3. Right-click the Add method, and select Create Unit Tests.

    The Create Unit Tests dialog box appears.

    In the Current selection pane, a tree structure shows the hierarchy of the classes and members of the assembly that contains the Form1 class. You can use this page to generate unit tests for any selection of those members, and to choose a test project into which you want the generated unit tests to be put.

    In the tree structure, only the Add method is selected. Leave it selected.

  4. For Output project, click Create a new Smart Device Visual C# test project.

    Note

    The language of the test project does not have to match the language of the project. For an example, you can create a Smart Device Visual C# test project on a Visual Basic device project.

  5. Click Settings.

    The Test Generation Settings dialog box appears. In Naming settings group, you can change the way test files, test classes, and test methods are named as they are generated. In the General group, you can change other aspects of test generation. Leave the default values for these settings and then click OK.

  6. In the Create Unit Tests dialog box, click OK.

    The New Test Project dialog box appears.

  7. Accept the default name and then click Create.

    This creates a project named TestProject1, which holds the unit test, a file named DeviceSample.vsmdi, which contains information about test lists and tests, and a file that is named SmartDeviceTestRun.testrunConfig, which contains information about where and how the test will run.

    To that test project is added a file that is named Form1Test.cs, which contains a test class.

    Note

    Every test method is automatically assigned the TestMethod() attribute. Each test corresponds to a single method in the code under test that you want to test. Test methods are in a test class that is assigned the TestClass() attribute.

  8. In Form1Test.cs, specify values for the variables to be tested. Scroll to the AddTest method, where you see three // TODO lines that indicate variables to set, namely a, b, and expected.

  9. Set the following values:

    int a = 4;
    int b = 5;
    int expected = 9;
    

    This specifies that a return value of 9 is expected when calling the method with values of 4 and 5.

  10. Comment out this Assert statement.

    Assert.Inconclusive("Verify the correctness of this test method.");
    

    This remaining Assert statement compares the expected (9) with the actual result from a call to the Add method of the Form1 class. If the two values are unequal, the Assert returns False, which makes the test fail.

  11. Save the Form1Tests.cs file.

    You have created a source-code file that contains tests for the DeviceSample project. You are now ready to run the tests in the Form1Test class on the code of the DeviceSample project.

View Run Configuration

To view run configuration

  1. Double-click the file that is named SmartDeviceTestRun.testrunConfig in Solution Explorer.

  2. Click Hosts.

  3. Verify that Host type is set to Smart Device, Platform is set to Pocket PC 2003, and Device is set to Pocket PC 2003 SE Emulator.

  4. Click Close to exit.

    You have viewed the run configuration which specifies the context for executing tests.

Run a Unit Test

To run and customize a unit test

  1. On the Test menu, point to Windows, and then click Test View.

    The Test View window appears.

  2. Right-click AddTest and then click Run Selection.

    Visual Studio deploys the test to the device emulator and runs the test. If the Test Results window is not already open, it opens now.

    In the Result column in the Test Results window, test status is displayed as Running when the test is running. After the test run finishes, the outcome of the test changes to Success.

    Note

    When you deploy tests, Visual Studio does not automatically deploy and install the .NET Compact Framework or SQL Server Compact 3.5 to the target device or emulator. Your emulator or device must have them installed before running a test. For more information, see How to: Install the .NET Compact Framework on a Device and How to: Install SQL Server Compact 3.5 on a Device.

  3. In the Test Results window, double-click the row that represents the test.

    This opens the test Results Details page, which contains information about the results of the test.

    You have run the generated AddTest method of your Form1Test test class and viewed the results.

Run a Unit Test from a Command Prompt

To run and customize a unit test

  1. Click Start, point to All Programs, point to Microsoft Visual Studio 2008, point to Visual Studio Tools, and then click Visual Studio 2008 Command Prompt.

    The command prompt opens to the folder: drive:\Program Files\Microsoft Visual Studio 9.0\VC.

  2. Change to the folder that contains the assembly built from your test project.

    To do this, first change to your solution folder. For the DeviceSample solution that was created in the prerequisite walkthrough, this folder is: drive:\Users\username\My Documents\Visual Studio\Projects\DeviceSample. Then change to the folder for your test project by typing the following command at the command prompt:

    cd TestProject1\bin\Debug

    Note

    You can also run MSTest.exe from any folder by specifying full paths.

    This folder contains the test project you created in the procedures for creating and running unit tests. The test project assembly, TestProject1.dll, contains a few unit tests.

    Note

    Your production code project and your test project will produce distinct assemblies. Make sure to run the command-line utility on the assembly of the test project, not on the assembly of your production code project.

  3. Use the command-line utility to test the application.

    Type the following at a command prompt:

    MSTest /testcontainer:TestProject1.dll /runconfig:..\..\..\SmartDevicetestrun.testrunconfig

    Note

    When running unit tests on smart devices, you must specify the /runconfig option.

    This command runs all tests in the assembly and returns results such as the following:

    Loading testproject1.dll...

    Starting execution...

    Results Top Level Tests

    ------- ---------------

    Passed TestProject1.Form1Test.AddTest

    1/1 test(s) Passed

    Summary

    -------

    Test Run Completed.

    Passed 1

    ---------

    Total 1

    Results file: <path>\<test run name>.trx

    Run Configuration: Default Run Configuration

    Note

    The .trx file generated by the execution of mstest.exe can be opened in Visual Studio for additional investigation of test results. For more information about the full range of options that you can use with the MSTest command, see MSTest.exe Command-Line Options.

    The MSTest.exe command-line utility is especially useful for automating test runs to be started in batch files or other utilities.

See Also

Other Resources

Working with Unit Tests

Testing Tools for Smart Device Projects