-
Notifications
You must be signed in to change notification settings - Fork 636
Writing Unit Test Libraries for Dynamo
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.
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.
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.
- DynamoTestUIBase - This will create a DynamoView (https://github.com/DynamoDS/Dynamo/blob/master/test/DynamoCoreWpfTests/ViewExtensions/WorkspaceDependencyViewExtensionTests.cs)
- UnitTestBase - For basic unit tests. (https://github.com/DynamoDS/Dynamo/blob/master/test/DynamoCoreTests/LibraryTests.cs)
- DSEvaluationUnitTestBase - Extends unit test base.
- DynamoModelTestBase - Creates an instance of just the DynamoModel and extends DSEvaluationUnitTestBase. (https://github.com/DynamoDS/Dynamo/blob/master/test/DynamoCoreTests/CodeBlockNodeTests.cs)
- DynamoViewModelUnitTest - This creates an instance of DynamoModel and DynamoViewModel. (https://github.com/DynamoDS/Dynamo/blob/master/test/DynamoCoreWpfTests/CustomNodeTests.cs)
- ProtoTestBase - Using this base class, you can run any language tests by executing a DS code and validating the output. (https://github.com/DynamoDS/Dynamo/blob/master/test/Engine/ProtoTest/Associative/MethodResolution.cs)
- RecordedUnitTestBase - Using this test base, you can run recorded tests by writing the commands in a xml file. (https://github.com/DynamoDS/Dynamo/blob/master/test/DynamoCoreWpfTests/RecordedTests.cs)
- GeometricTestBase - For unit tests that can use the geometry library. (https://github.com/DynamoDS/Dynamo/blob/master/test/Libraries/CoreNodesTests/ColorTests.cs)
- SystemTestBase - For system tests that start Dynamo and can use .dyn files. (https://github.com/DynamoDS/Dynamo/blob/master/test/DynamoCoreWpfTests/RunSettingsTests.cs)
- RevitNodeTestBase - For unit tests, runnable on Revit through RTF.
- RevitSystemTestBase - For system tests, runnable on Revit through RTF.
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:
- Open the Solution Explorer in Visual Studio
- Right click on "References" in your Project.
- Select "Manage Nuget Packages". The NuGet Package Manager interface will appear.
- Select "Browse".
- In the search field, type "DynamoVisualProgramming". The Dynamo NuGets will appear.
- Select "DynamoVisualProgramming.Tests" and click the "Install" button.
- 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.
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:
- Make your test fixture class inherit from
GeometricTestBase
. Be careful not to override theSetup
orTearDown
methods, as the implementations of these methods on the base class handle the configuration of the geometry library. - 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 areVersion220
,Version221
andVersion223
(for Dynamo 2.0 only). -
⚠️ , For version 224 and later - you should specifyRequestedLibraryVersion2
as the key, its value should be a version number with major,minor and build components. ie224.4.0
-
For testing expectations, refer to this wiki page: https://github.com/DynamoDS/Dynamo/wiki/Testing-expectations
Looking for help with using the Dynamo application? Try dynamobim.org.
- Dynamo 2.0 Language Changes Explained
- How Replication and Replication Guide work: Part 1
- How Replication and Replication Guide work: Part 2
- How Replication and Replication Guide work: Part 3