Skip to content

Writing Unit Test Libraries for Dynamo

Ian Keough edited this page Mar 10, 2016 · 1 revision

This article provides all the information you need to create unit tests for your Dynamo library. Dynamo uses the NUnit framework for all of its unit testing. Additionally, the Revit Test Framework has been developed to enable unit testing on top of Revit.

There are two types of tests that you will find in the Dynamo project, and which you might want to make for your library, unit tests and system tests. Unit tests test small "units" of code, like methods, while system tests test the entire system similar to the way in which a user would use it. System tests typically create a Dynamo session, open a Dynamo file, evaluate it, and ensure that the returned results are what you'd expect.

There are thousands of tests available in the Dynamo source if you need to see how to write a test. For example, CoreNodesTests contains unit tests for Dynamo's core node library. WorkflowTests contains unit tests for some of Dynamo's samples.

###Test Libraries Several libraries are provided with Dynamo which contain base classes and utility methods to facilitate testing.

  • TestServices - Contains the base class for Dynamo unit tests.
  • SystemTestServices - Contains the base class for Dynamo system tests.

###Base Classes Several base classes are provided to facilitate writing tests for Dynamo. Using these base classes means you don't have to worry about things like initializing Dynamo and its geometry library for units tests, or providing access to the Dynamo workspace in system tests.

  • GeometricTestBase - For unit tests that can use the geometry library.
  • SystemTestBase - For system tests that start Dynamo and can use .dyn files.
  • RevitNodeTestBase - For unit tests, runnable on Revit through RTF.
  • RevitSystemTestBase - For system tests, runnable on Revit through RTF.

###Setting Up Your Dynamo Package for Testing in Visual Studio

Because your tests will rely on Dynamo libraries, you'll need to reference Dynamo libraries in your project. The preferred route for testing packages is to use the DynamoVisualProgramming.Tests NuGet package. This package contains the necessary assemblies to support unit and system testing. This package is available on nuget.org. To install it in Visual Studio 2015:

  1. Open the Solution Explorer in Visual Studio
  2. Right click on "References" in your Project.
  3. Select "Manage Nuget Packages". The NuGet Package Manager interface will appear.
  4. Select "Browse".
  5. In the search field, type "DynamoVisualProgramming". The Dynamo NuGets will appear.
  6. Select "DynamoVisualProgramming.Tests" and click the "Install" button.

By default, NuGet will install the latest "stable" version of the package. NuGet package versions correspond to versions of Dynamo. Be sure to pick the NuGet packages with the version that corresponds to the version of Dynamo against which you want to test.

###Geometry Testing If your tests create geometry using the Dynamo geometry library, then you'll need to do some additional configuration to ensure that your tests function properly. Typically, the Dynamo application takes care of starting and stopping the geometry library. For unit testing, we won't be using Dynamo, so your tests will need to start and stop the geometry library. Fortunately, we have a testing base class, GeometricTestBase, that is built for this purpose. To test geometry, you will need to do the following:

  1. Make your test fixture class inherit from GeometricTestBase. Be careful not to override the Setup or TearDown methods, as the implementations of these methods on the base class handle the configuration of the geometry library.
  2. Include a TestServices.dll.config file in your build output directory. This file will specify the base directory for Dynamo. A 'TestServices.dll.config` file looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="DynamoBasePath" value="<Your Dynamo Directory>"/>
    <add key="RequestedLibraryVersion" value="Version221"/>
  </appSettings>
</configuration>
  • DynamoBasePath corresponds to the location of Dynamo on your machine. This can be any version of Dynamo including Dynamo for Revit or Dynamo Studio, or a build directory, if you're building Dynamo from source. This config file is required so that Dynamo can find the geometry library assemblies, which are not included as part of the NuGet.
  • RequestedLibraryVersion This specifies the version of ASM that the geometry library will load. The acceptable values are Version220 and Version221.
Clone this wiki locally