|
1 | | -# Revit Injector |
| 1 | +<p align="center"> |
| 2 | + <picture> |
| 3 | + <source media="(prefers-color-scheme: dark)" width="610" srcset="https://github.com/user-attachments/assets/e539b207-f469-46ed-8543-5839146761ec"> |
| 4 | + <img alt="RevitUnit" width="610" src="https://github.com/user-attachments/assets/297430e2-7f9c-498a-8ae5-dfd48caae621"> |
| 5 | + </picture> |
| 6 | +</p> |
2 | 7 |
|
3 | | -This repository contains a tool for injecting dlls into Revit process. |
| 8 | +## Unit testing framework for Revit API |
| 9 | + |
| 10 | +This library provides a testing shell for Revit add-ins using a Microsoft testing platform. |
4 | 11 |
|
5 | 12 | ## Installation |
6 | 13 |
|
7 | | -Add a new Nuget source: |
| 14 | +You can install the Toolkit as a [NuGet package](https://www.nuget.org/packages/Nice3point.TUnit.Revit). |
| 15 | + |
| 16 | +The packages are compiled for specific versions of Revit. To support different versions of libraries in one project, use the `RevitVersion` property: |
| 17 | + |
| 18 | +```xml |
| 19 | + |
| 20 | +<PackageReference Include="Nice3point.TUnit.Revit" Version="$(RevitVersion).*"/> |
| 21 | +``` |
| 22 | + |
| 23 | +> [!WARNING] |
| 24 | +> The public version of the package does not contain implementation for the framework. |
| 25 | +> An open source version is not currently planned due to Autodesk export regulations. |
| 26 | +
|
| 27 | +## Writing your first test |
| 28 | + |
| 29 | +Start by creating a new class inheriting from `RevitApiTest`: |
| 30 | + |
| 31 | +```csharp |
| 32 | +public class MyTestClass : RevitApiTest |
| 33 | +{ |
| 34 | + |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +Add a method with `[Test]` and `[TestExecutor<RevitThreadExecutor>]` attributes: |
| 39 | + |
| 40 | +```csharp |
| 41 | +public class MyTestClass : RevitApiTest |
| 42 | +{ |
| 43 | + [Test] |
| 44 | + [TestExecutor<RevitThreadExecutor>] |
| 45 | + public async Task MyTest() |
| 46 | + { |
| 47 | + |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +This is your runnable test. The `[TestExecutor<RevitThreadExecutor>]` attribute ensures the test executes within Revit's single-threaded API context. |
| 53 | + |
| 54 | +## Running your tests |
| 55 | + |
| 56 | +This library uses TUnit, which is built on top of the Microsoft.Testing.Platform. Combined with source-generated tests, running your tests is available in multiple ways. |
| 57 | + |
| 58 | +### dotnet run |
8 | 59 |
|
9 | | -```text |
10 | | -<configuration> |
11 | | - <packageSources> |
12 | | - <add key="Nice3point" value="https://nuget.pkg.github.com/Nice3point/index.json" /> |
13 | | - </packageSources> |
14 | | -</configuration> |
| 60 | +For simple project execution, `dotnet run` is the preferred method, allowing easier command line flag passing. |
| 61 | + |
| 62 | +```bash |
| 63 | +cd 'C:/Your/Test/Directory' |
| 64 | +dotnet run -c "Release R26" |
| 65 | +``` |
| 66 | + |
| 67 | +### dotnet test |
| 68 | + |
| 69 | +`dotnet test` requires the configuration to target the desired Revit version. |
| 70 | + |
| 71 | +```shell |
| 72 | +cd 'C:/Your/Test/Directory' |
| 73 | +dotnet test -c "Release R26" |
| 74 | +``` |
| 75 | + |
| 76 | +### dotnet exec |
| 77 | + |
| 78 | +If your test project has already been built, use `dotnet exec` or `dotnet` with the .dll path: |
| 79 | + |
| 80 | +```shell |
| 81 | +cd 'C:/Your/Test/Directory/bin/Release R26/' |
| 82 | +dotnet exec YourTestProject.dll |
| 83 | +``` |
| 84 | + |
| 85 | +or |
| 86 | + |
| 87 | +```shell |
| 88 | +cd 'C:/Your/Test/Directory/bin/Release R26/' |
| 89 | +dotnet YourTestProject.dll |
15 | 90 | ``` |
16 | 91 |
|
17 | | -Install Injector as a nuget package: |
| 92 | +## Application testing |
| 93 | + |
| 94 | +Test Revit application-level functionality: |
| 95 | + |
| 96 | +```csharp |
| 97 | +public sealed class RevitApplicationTests : RevitApiTest |
| 98 | +{ |
| 99 | + [Test] |
| 100 | + [TestExecutor<RevitThreadExecutor>] |
| 101 | + public async Task Documents_Startup_IsEmpty() |
| 102 | + { |
| 103 | + var documents = Application.Documents.Cast<Document>(); |
| 104 | + |
| 105 | + await Assert.That(documents).IsEmpty(); |
| 106 | + } |
18 | 107 |
|
19 | | -```text |
20 | | -<PackageReference Include="Nice3point.Revit.Injector" Version="$(RevitVersion).*"/> |
| 108 | + [Test] |
| 109 | + [TestExecutor<RevitThreadExecutor>] |
| 110 | + public async Task Create_XYZ_ValidDistance() |
| 111 | + { |
| 112 | + var point = Application.Create.NewXYZ(3, 4, 5); |
| 113 | + |
| 114 | + await Assert.That(point.DistanceTo(XYZ.Zero)).IsEqualTo(7).Within(0.1); |
| 115 | + } |
| 116 | +} |
21 | 117 | ``` |
22 | 118 |
|
23 | | -## Usages |
| 119 | +## Document testing |
| 120 | + |
| 121 | +Test document-specific operations with setup and cleanup: |
24 | 122 |
|
25 | 123 | ```csharp |
26 | | -var injector = new Injector(); |
| 124 | +public sealed class RevitDocumentTests : RevitApiTest |
| 125 | +{ |
| 126 | + private static Document _documentFile = null!; |
| 127 | + |
| 128 | + [Before(Class)] |
| 129 | + [HookExecutor<RevitThreadExecutor>] |
| 130 | + public static void Setup() |
| 131 | + { |
| 132 | + _documentFile = Application.OpenDocumentFile($@"C:\Program Files\Autodesk\Revit {Application.VersionNumber}\Samples\rac_basic_sample_family.rfa"); |
| 133 | + } |
| 134 | + |
| 135 | + [After(Class)] |
| 136 | + [HookExecutor<RevitThreadExecutor>] |
| 137 | + public static void Cleanup() |
| 138 | + { |
| 139 | + _documentFile.Close(false); |
| 140 | + } |
| 141 | + |
| 142 | + [Test] |
| 143 | + [NotInParallel] |
| 144 | + [TestExecutor<RevitThreadExecutor>] |
| 145 | + public async Task FilteredElementCollector_ElementTypes_ValidAssignable() |
| 146 | + { |
| 147 | + var elements = new FilteredElementCollector(_documentFile) |
| 148 | + .WhereElementIsElementType() |
| 149 | + .ToElements(); |
| 150 | + |
| 151 | + using (Assert.Multiple()) |
| 152 | + { |
| 153 | + await Assert.That(elements).IsNotEmpty(); |
| 154 | + await Assert.That(elements).All().Satisfy(element => element.IsAssignableTo<ElementType>()); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + [Test] |
| 159 | + [NotInParallel] |
| 160 | + [TestExecutor<RevitThreadExecutor>] |
| 161 | + public async Task Delete_Dimensions_ElementsWithDependenciesDeleted() |
| 162 | + { |
| 163 | + var elementIds = new FilteredElementCollector(_documentFile) |
| 164 | + .WhereElementIsNotElementType() |
| 165 | + .OfCategory(BuiltInCategory.OST_Dimensions) |
| 166 | + .OfClass(typeof(RadialDimension)) |
| 167 | + .ToElementIds(); |
27 | 168 |
|
28 | | -// Injects the current dll into the Revit process and returns the application instance |
29 | | -var application = injector.InjectApplication(); |
| 169 | + using var transaction = new Transaction(_documentFile); |
| 170 | + transaction.Start("Delete dimensions"); |
| 171 | + var deletedElements = _documentFile.Delete(elementIds); |
| 172 | + transaction.Commit(); |
30 | 173 |
|
31 | | -//Properly cleans up and ejects the application from the Revit process |
32 | | -injector.EjectApplication(); |
| 174 | + await Assert.That(deletedElements.Count).IsGreaterThanOrEqualTo(elementIds.Count); |
| 175 | + } |
| 176 | +} |
33 | 177 | ``` |
0 commit comments