Skip to content

Writing Unit Test Libraries for Dynamo

reddyashish edited this page Feb 24, 2022 · 8 revisions

This article provides all the information you need to create unit tests for your Dynamo library. Dynamo uses the NUnit framework version 2 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.

Setting Up Your Dynamo Package for Testing in Visual Studio

⚠️ Before you write your unit tests for Dynamo, make sure you've installed the NUnit 2 Visual Studio adapter, which is the preferred way to run the unit tests. Note NUnit adapter version 3 will not work. Make sure to set the adapter runtime to x64

screen shot 2018-07-18 at 11 13 46 am

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.
  7. Check that the NUnit package has also installed and is version 2.6.3 and not 3.x .

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="RequestedLibraryVersion2" value="226.0.0"/>
   <!-- legacy Dynamo version syntax <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, Version221 and Version223 (for Dynamo 2.0 only).

  • ⚠️, For version 224 and later - you should specify RequestedLibraryVersion2 as the key, its value should be a version number with major,minor and build components. ie 224.4.0

  • For testing expectations, refer to this wiki page: https://github.com/DynamoDS/Dynamo/wiki/Testing-expectations

Releases

Roadmap

How To

Dynamo Internals

Contributing

Python3 Upgrade Work

Libraries

FAQs

API and Dynamo Nodes

Clone this wiki locally