Creating Unit Tests for Operations

Unit tests let you create an automated cycle of tests to ensure that your service behaves as expected. DCS provides a Visual Studio extension that lets you generate unit tests for DCS service implementations

Note

The DCS Unit Test Visual Studio extension requires Microsoft Visual Studio Team Studio 2008 Team Suite.

Before you can implement a unit test, you must deploy the service and create a proxy object. You can deploy the service by using the Deploy Assembly recipe of the DCS Software Factory, or manually by using the DCS deployment utilities. For more information, see Deploying DCS Services.

Note

You can create a unit test project early in the development cycle and use that project to test your service implementation incrementally. You must have one operation and task implementation in your service project before you create a test operation.

To create a unit test project

  1. In Visual Studio, in Solution Explorer, right-click the Tests folder, point to Add, and then click Test.
  2. In the Add New Project dialog box, under Templates, make sure that Test is selected. Provide a name and location for the unit test project, and then click OK.
  3. In the Configure Project dialog box, provide a namespace for the test project, and then click Finish.

Visual Studio generates a default unit test project with your specified details.

To create a new unit test

  1. Right-click the unit test project node, and then click Add Unit Test.

  2. In the Add Unit Test dialog box, on the SelectProject page, in the Projects list, select the project that contains the operation that you want to test.

  3. Note

    You can use this dialog only to create tests for operations in projects in the current solution.

  4. In the Envelopes list, select the workflow envelope that you want to test, and then click Next.

  5. Note

    Each workflow task implementation in DCS comprises a workflow file and a workflow envelope file that contains the code to initialize the workflowHost object. To test an operation, you select the envelope file that relates to the workflow that you want to test.

  6. On the WF Assembly References Selector page, make sure that the dialog has correctly identified the required assemblies, and then click Finish.

Visual Studio generates a template test class and an application configuration file that enables the test project to invoke DCS operations. You must customize both the application configuration file and unit test file to enable your test to execute successfully.

To configure the unit test project configuration file

  1. In Visual Studio, in Solution Explorer, in the unit test project, double-click app.config to open the file in the Text and Code Editor.

  2. In the <cis.channelFactory> element, locate the mexDiscoveryEndpoint element and modify the address attribute value to include the Discovery Service URL that the test project should use to invoke operations.

  3. Note

    If you implement a custom scope provider and ranker or any other custom extension of common DCS objects, you may need to modify the App.config file to reference your custom object implementations. For more information, see Extending DCS Functionality.

  4. Save and close the App.config file.

The template DCS test class contains a single method to test the operation that you select. The test method contains the required code to call a DCS operation. The method creates a context object and specifies scope, creates a proxy object for the service named target, and creates a request message to send in the test operation invocation. The method also creates two response message instances named expected and actual. After creating these objects, the class invokes the operation on the target service object by passing the request and context objects as parameters, and stores the invocation response in the actual object. The final line of the method compares the expected and actual response message objects and verifies that they are the same by using an Assert statement.

Note

The template test method compares response message objects for equality.

The following code example shows the template-generated test method for an operation named GetTemperatureInFahrenheit. The GetTemperatureInFahrenheit operation accepts a request message containing a city name and returns a double representing the temperature of the city. For detailed instructions of how to implement the GetTemperatureInFahrenheit operation, see Walkthrough: Implementing a Service by Using a Category Workflow.

public void GetTemperatureInFahrenheitTest()
{
    Context context = new Context();
    context.Environment = "DEMO";
    context.Organization = new Organization();
    context.Organization.Plant = "DEMO";

    global::WeatherInformationSpecificProxy.WeatherInformationServiceProxy target = new global::WeatherInformationSpecificProxy.WeatherInformationServiceProxy();

    global::WeatherInformationNamespace.Messages.GetTemperatureRequest request = null; // TODO: Initialize to an appropriate value

    global::WeatherInformationNamespace.Messages.GetTemperatureResponse expected = null;
    global::WeatherInformationNamespace.Messages.GetTemperatureResponse actual;

    actual = target. GetTemperatureInFahrenheit(request, context);

    Assert.AreEqual(expected, actual, "GetTemperatureInFahrenheit did not return the expected value.");
}

To customize a template unit test class

  1. In Visual Studio, in Solution Explorer, in the unit test project, double-click the unit test class that you want to modify.
  2. In the test class, locate the test method for the operation (it will be named <operationName>Test, where<operationName> is the name of the operation under test).
  3. Modify the context object to configure the required scope. For more information about scopes, see DCS Scopes.
  4. Modify the request message object to contain the required test data to pass to the service implementation.
  5. Modify the expected object to contain the expected test result information.
  6. If you require a different evaluation of test success, modify or supplement the Assert statement and the end of the test method with additional conditions. You can also modify the response message returned by the Assert statement if it fails.
  7. Save the customized test class, and build the project.

The following code sample shows a customized test method for the GetTemperatureInFahrenheit operation with changes shown in bold font. This sample customizes the scope, request, and expected objects and modifies the Assert statement to evaluate properties of the response message rather than the entire message instance.

public void GetTemperatureInFahrenheitTest()
{
    Context context = new Context(); 
    //context.Environment = "DEMO"; - removed; not required for deployed service
    context.Organization = new Organization();
    context.Organization.Plant = "TemperatureService";

    global::WeatherInformationSpecificProxy.WeatherInformationServiceProxy target = new global::WeatherInformationSpecificProxy.WeatherInformationServiceProxy();

    WeatherInformationNamespace.Messages.GetTemperatureRequest testRequest = new WeatherInformationNamespace.Messages.GetTemperatureRequest();
    testRequest.CityName = "London";

    global::WeatherInformationNamespace.Messages.GetTemperatureRequest request = testRequest; 

    WeatherInformationNamespace.Messages.GetTemperatureResponse testResponse = new WeatherInformationNamespace.Messages.GetTemperatureResponse();
    testResponse.Temperature = 73.4;

    global::WeatherInformationNamespace.Messages.GetTemperatureResponse expected = testResponse;
    global::WeatherInformationNamespace.Messages.GetTemperatureResponse actual;

    actual = target.GetTemperatureInFahrenheit(request, context);

    Assert.AreEqual(expected.Temperature, actual.Temperature, "GetTemperatureInFahrenheit did not return the expected Temperature value."); 
}

To execute a completed unit test

  1. In Visual Studio, on the Test menu, point to Windows, and then click Test Manager.
  2. Select the check boxes related to the tests you want to run, and then click the Run Checked Tests button.
  3. Visual Studio runs the selected tests and displays the results in the TestResults pane.

See Also

Deploying DCS Services

Extending DCS Functionality

DCS Scopes