Git Product home page Git Product logo

xunitrevit's Introduction

xUnitRevit

Build

xunit2

Twitter Follow Community forum users website docs

Introduction

An xUnit runner for Autodesk Revit.

Check out our blog post on this ๐Ÿ‘‰ https://speckle.systems/blog/xunitrevit !

xUnitRevit uses speckle.xunit.runner.wpf which is a fork of xunit.runner.wpf, it allows to easily develop and run xUnit tests in Revit.

Many thanks to all the developers of xunit and xunit.runner.wpf!

Structure

This repo is composed of 2 projects:

  • xUnitRevitRunner: the actual Revit addin
  • xUnitRevitUtils: a utility library to help pass Revit data to the test libraries when running the tests

Getting Started

There are very few steps required to create and run your fist unit tests with xUnitRevit:

  1. create a copy of the config sample file and re-name the copy to config.json
  2. follow the instructions here to set up the config file
  3. build/install xUnitRevitRunner
  4. create a test library
  5. start Revit, launch the xUnitRevitRunner addin and select the test library
  6. done! Add a star โญ to our repo if it was useful ๐Ÿ˜‰

Building/installing xUnitRevit

After cloning this repo, all you need to do to run xUnitRevitRunner is to build the project corresponding to your revit version in Debug mode

This will build the project and copy its dlls to the Revit addin folder %appdata%\Autodesk\Revit\Addins.

You can also, similarly, build the project in Release mode, and manually copy the built files from xunit-Revit\Release.

Creating a test library

Creating a test library is pretty straightforward, at least we tried to make it as simple as possible!

Just follow the steps below for Revit 2021:

  • create a new .net framework class library project (4.8 for Revit 2021)
  • add the NuGet packages
    • xunit
    • xUnitRevitUtils.2021

That's it, now we can start adding our tests.

Writing a simple test

To do almost anything with the Revit API you need a reference to the active Document, and this is where xUnitRevitUtils comes into play, with its xru static class. The code below shows how we can use it to get a list of Walls and check their properties.

Full code : https://github.com/Speckle-Next/xUnitRevit/blob/master/SampleLibrary/SampleTest.cs

  [Fact]
public void WallsHaveVolume()
{
  var testModel = GetTestModel("walls.rvt");
  var doc = xru.OpenDoc(testModel);

  var walls = new FilteredElementCollector(doc).WhereElementIsNotElementType().OfCategory(BuiltInCategory.OST_Walls).ToElements();

  foreach(var wall in walls)
  {
    var volumeParam = wall.get_Parameter(BuiltInParameter.HOST_VOLUME_COMPUTED);
    Assert.NotNull(volumeParam);
    Assert.True(volumeParam.AsDouble() > 0);
  }
  doc.Close(false);
}

Writing tests with fixtures

To be able to share context between tests, xUnits uses fixtures. We can use fixtures for instance, to open a Revit model only once and use it across multiple tests.

Let's see an example, full code: https://github.com/Speckle-Next/xUnitRevit/blob/master/SampleLibrary/TestWithFixture.cs

public class DocFixture : IDisposable
{
  public Document Doc { get; set; }
  public IList<Element> Walls { get; set; }


  public DocFixture()
  {
    var testModel = Utils.GetTestModel("walls.rvt");
    Doc = xru.OpenDoc(testModel);

    Walls = new FilteredElementCollector(Doc).WhereElementIsNotElementType().OfCategory(BuiltInCategory.OST_Walls).ToElements();
  }

  public void Dispose()
  {
  }
}
public class TestWithFixture : IClassFixture<DocFixture>
{
  DocFixture fixture; 
  public TestWithFixture(DocFixture fixture)
  {
    this.fixture = fixture;
  }

  [Fact]
  public void CountWalls()
  {
    Assert.Equal(4, fixture.Walls.Count);
  }

  [Fact]
  public void WallOffset()
  {
    var wall = fixture.Doc.GetElement(new ElementId(346573));
    var param = wall.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET);
    var baseOffset = UnitUtils.ConvertFromInternalUnits(param.AsDouble(), param.DisplayUnitType);

    Assert.Equal(2000, baseOffset);
  }
}

Writing test that use Revit transactions

Another feature of xUnitRevitUtils is that it offers a helper method to run Transactions, so you don't have to worry about that ๐Ÿคฏ! Check the example below: https://github.com/Speckle-Next/xUnitRevit/blob/master/SampleLibrary/TestWithFixture.cs

[Fact]
public void MoveWallsUp()
{
  var walls = fixture.Walls.Where(x => x.Id.IntegerValue != 346573);

  xru.RunInTransaction(() =>
  {
    foreach(var wall in walls)
    {
      var param = wall.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET);
      var baseOffset = UnitUtils.ConvertToInternalUnits(2000, param.DisplayUnitType);
      param.Set(baseOffset);
    }
  }, fixture.Doc)
  .Wait(); // Important! Wait for action to finish

  foreach (var wall in walls)
  {
    var param = wall.get_Parameter(BuiltInParameter.WALL_BASE_OFFSET);
    var baseOffset = UnitUtils.ConvertFromInternalUnits(param.AsDouble(), param.DisplayUnitType);
    Assert.Equal(2000, baseOffset);
  }
}

image

Additional Notes

Configuration

We've added a couple of optional settings for lazy developers like me, to help speed up frequent testing of a test library. You'll see a config_sample.json in the root of the project. Copy the file and rename the copy to config.json and set it to copy local = true. You'll then be able to configure

  • startupAssemblies: if set, automatically loads a set of assemblies when xUnitRevit starts
  • autoStart: if true, automatically opens the xUnitRevit window after Revit loads

Dll locking

Dlls loaded by xUnitRevit are loaded in Revit's AppDomain, and therefore it's not possible to recompile them until Revit is closed (even if you see an auto reload option in the UI). But don't despair, since Revit 2020 it's possible to edit & continue your code while debugging, so you won't have to restart Revit each time.

Next steps

As for next steps, we're planning to add additional features to run xUnitRevit from a CI/CD routine.

Stay tuned!

Contributing

xUnitRevit was developed to help us develop a better Speckle 2.0 connector for Revit, we hope you'll find it useful too.

Want to suggest a feature, report a bug, submit a PR? Please open an issue to discuss first!

Please make sure you read the contribution guidelines and code of conduct for an overview of the practices we try to follow.

Community

The Speckle Community hangs out on the forum, do join and introduce yourself & feel free to ask us questions!

Security

For any security vulnerabilities or concerns, please contact us directly at security[at]speckle.systems.

License

Unless otherwise described, the code in this repository is licensed under the MIT License. Please note that some modules, extensions or code herein might be otherwise licensed. This is indicated either in the root of the containing folder under a different license file, or in the respective file's header. If you have any questions, don't hesitate to get in touch with us via email.

xunitrevit's People

Contributors

alanrynne avatar alexeyzarubin avatar chris-welch-aurecon avatar connorivy avatar didimitrie avatar dre-tas avatar iainsproat avatar izzylys avatar komori556 avatar konradzaremba avatar martijn00 avatar takazoe avatar teocomi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

xunitrevit's Issues

Wrap the task returned by xru.RunInTranslation to automatically wait

Hey there,

Breaking change, but just got caught up again by not having Wait() at the end of the xru task. I'm not 100% clear on whats happening here, but if we always have to wait for the tasks to work with revit, can we just automatically append it to prevent user error, especially when the failures are hard to diagnose as they 'fail' silently?

Unable to build

Hey guys! Thanks for the effort. Really appreciated!!

I am looking forward to use this project to test my app, but so far I am failing at building it for Revit 2020 (or any other version).
I'm following the instructions in the readme, but I'm always getting this error and I'm not too sure what it means / what to do about it:
image

I cloned the project, I selected Debug2020 as the mode and when I hit Start or Build I get this error. I get the same error is I use the Release mode.

What am I doing wrong?

Cheers

Revit crashing when reading assembly in modeless dialog (Revit 2020)

Dear xUnitRevit-Team,
first of all thank you very much for your great work, i appreciate a lot!
Unfortunately i encountered an issue when configuring xUnitRevit for Revit 2020: The modeless dialog makes Revit crashes when i read in the .dll of the modeless dialog. Revit crashes without any usable error message...

I used the sampleLibrary and exchanged the TestModel to a Revit-2020 model that has four walls and two slabs.
I created the .dll having installed the NuGet-Packages for xUnit and xUnitRevitUtils2020.

Would anyone have an advise for me? Also a more minimalist test sample i would appreciate, to ensure that the workflow in general works.

Thank you very much!
Kind Regards

Test automation

Thanks to @Mehdi-Lucas, who provided a fork that used version 2.7.0, I was able to get it working.
Now when starting Revit manually, I am able to load the test dll into the Test runner, and execute the tests. When I attach a debugger, I am even able to debug - which is great so far!

I have one question regarding test automation:
Is there are way to load and run the tests via command line (e.g. dotnet test or something else) while Revit is open?
I know that the Revit API only can be accessed if Revit is running , so the question is if there is a way to get some test automation set up using this project.?
Or to rephrase it a bit differently: What would be the best way if I want to run my tests automated, without manual interaction?

Thanks for your support!

Support for Revit 2024

Prerequisites

What package are you referring to?

  • xUnitRevitRunner
  • xUnitRevitUtils

Is your feature request related to a problem? Please describe.

Currently, it only supports versions up to Revit 2023, and cannot be used with the latest Revit 2024

Describe the solution you'd like

Add support for Revit 2024

Describe alternatives you've considered

Additional context

Since Speckle.Revit.API 2024.0.0 already exists, it can be easily adapted for use
animation

Related issues or community discussions

*I'm not good at English, so I'm using ChatGPT to write this issue. I apologize if it is difficult to understand.

Update .gitmodules to use https url instead of ssh

Can you change url of .gitmodules from [email protected]:specklesystems/speckle.xunit.runner.wpf.git to https://github.com/specklesystems/xUnitRevit.git? I got authentication issues trying to clone the repo using Github Desktop. I am sure I could figure out how to create and use a ssh public key but others will probably have similar issues trying to clone repo. Below is what the .gitmodules would change to.

Current

[submodule "speckle.xunit.runner.wpf"]
	path = speckle.xunit.runner.wpf
	url = [email protected]:specklesystems/speckle.xunit.runner.wpf.git

Suggested

[submodule "speckle.xunit.runner.wpf"]
	path = speckle.xunit.runner.wpf
	url = https://github.com/specklesystems/xUnitRevit.git

No test show up after loading a test assembly

Prerequisites

What package are you referring to?

xUnitRevit

Describe the bug

I was hoping that you could help me with this issue:
I cloned and built xUnitRevit the add-in for Revit 2022 I tried to create a sample test assembly to load on the plug in and run.

The bug is: I can load the .dll inside xUniRevit but I see no test cases.

I tried with the SampleLibrary provided in the repo, as well with creating a new project tagetting .NET Framework 4.8 and basically copying one test inside a class.

image

To Reproduce

Launch Revit

(No document is open)

Go to Add-ins tab, launch Test Runner

Assembly -> Open -> Go to \xUnitRevit\SampleLibrary\bin\Debug\SampleLibrary.dll

Expected behavior

Some test cases would show on the window.

Screenshots

System Info

If applicable, please fill in the below details - they help a lot!

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Additional context

Proposed Solution (if any)

Optional: Affected Projects

How to get it running?

Hi Team,

thanks for creating and sharing the xunit test runner framework for Revit.
I think I followed each step in your documentation as described. (Though I still feel that some things are missing in the instructions).
However, the add-in is installed, it shows up in Revit, and I am also able to build the sample test project.
In Revit, the main window of the Test Runner add-in opens up, but unfortunately, I am not able to do something meaningful here. I tried to add different test assemblies, but nothing happened. It basically stays blank.
I went through the past issues, and I tried it with previous versions of the test runner and xunit, but without success.
Any ideas what I could be doing wrong?

Also, I have another question: Is it possible to run a test in "Debug" mode? So that I could set a breakpoint in Visual Studio when running a test, and that the breakpoint would be hit?

Thanks for your support,

Andreas

Suggestion - Get Revit context when testing

Hi guys! It's-a me again ๐Ÿ˜„

Not a bug, just a suggestion/request that I'd like to run through you to see if it makes sense.

Would it make sense to expose the context where the test runner is operating (i.e. the Application in Revit terms)?

This is my case: I created an object called RevitModel, that in some aspects wraps up Revit's Document object, but has a few other properties/methods.
One of the methods creates a new Revit file with Application.NewProjectDocument(string templatePath), which is a method of the Application object.
So I can't really test my method that creates a RevitModel because I can't get the Application that is running the test runner.

So what I'm thinking is, can we expose publicly xru.Uiapp so that it can be used to create objects? Or has it been set to private because that's the way it's has to be for some reason (like I am doing something that I'm not supposed to do in unit testing)?

Output should show full stacktrace

It'd be nice to see the full stacktrace in the tool! Currently I think it only is reporting the top level exception. It can just be appended to the bottom of the current text field.

Can't run tests

Steps taken

  • Cloned xUnitRevit
  • Restored packages
  • Built SampleLibrary and xUnitRevit
  • Launched Revit2020 and opened xUnitRevit
  • Loaded SampleLibrary.dll

"Run All" and "Run Selected" are always greyed out.

Not sure what other information I can provide, but had a friend try also and they had the same issue.

image

xunit 2.4.2 - tests not showing in Revit

I've written a few tests and tried to run them in Revit but they didn't show.
After some testing, I've found out that this was caused by the xunit 2.4.2. After downgrading to 2.4.1 all works fine.

Enhancement: Function to open RevitServer and CloudModels with xUnitRevit

We needed to be able to open models from our Revit server with xUnitRevit and couldn't find a solution in the existing repo.

This is now the only way to open a model:

/// <summary>
/// Opens and activates a document if not open already
/// </summary>
/// <param name="filePath">Path to the file to open</param>
public static Document OpenDoc(string filePath)
{
  Assert.NotNull(Uiapp);
  Document doc = null;
  //OpenAndActivateDocument only works if run from the current context
  UiContext.Send(x => doc = Uiapp.OpenAndActivateDocument(filePath).Document, null);
  Assert.NotNull(doc);
  return doc;
}

So we added the OpenDoc() function with the correct parameters for cloud/server models from the Revit API to the xru class in the xUnitRevitUtils:

/// <summary>
/// Opens and activates a document from server or cloud
/// </summary>
/// <param name="filePath">Path to the file to open</param>
/// <param name="openOpts">Opening Options for Cloud Models</param>
/// <param name="detach">Boolean to determine if file should be detached from central</param>
public static Document OpenDoc(string filePath, OpenOptions openOpts, Boolean detach)
{
  Assert.NotNull(Uiapp);
  Document doc = null;
  //OpenAndActivateDocument only works if run from the current context
  UiContext.Send(x => doc = Uiapp.OpenAndActivateDocument(ModelPathUtils.ConvertUserVisiblePathToModelPath(filePath), openOpts, detach).Document, null);
  Assert.NotNull(doc);
  return doc;
}

Do more people feel the need for this feature? If so, we would prepare a PR for it.

v1.0.3 failing tests involving RunInTransaction

I upgraded the xUnitRevitUtils package in my solution to 1.0.3 from NuGet and all the tests that had an xru.RunInTransaction() are now failing.

I then went back to my build of the dll (which has the context and uiapp exposed but not the logic to suppress warnings), overriding the dll from NuGet, and it works fine.
Also, when I tried to use RunInTransaction from 1.0.3 I didn't set ignoreWarnings = true, but it was still failing.

You might want to have a look at that. Again, I'm not sure why this is happening but my suspects are on the logic to suppress warnings.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.