第三方软件 API (AutoCAD) 单元测试的最佳实践

发布于 2024-07-13 21:46:28 字数 734 浏览 7 评论 0原文

我们正在开发在 AutoCAD 中使用的应用程序。 基本上,我们创建一个类库项目,并使用命令 (NETLOAD) 在 AutoCAD 中加载 .dll。

因此,我们可以使用命令、“调色板”、用户控件、表单等...

AutoDesk 通过一些 dll 提供 API,在其程序目录中运行。 引用这些 dll 时,您只能在运行时在 AutoCAD 中加载应用程序时调用 dll(这是 AutoDesk 的许可安全性)。

对于我们来说,在开发时,这不是问题,我们需要在 AutoCAD 上下文中进行直观测试,因此我们只需设置“调试属性”,以便它们启动 acad.exe 并使用 acad.exe 参数中的脚本加载我们的 dll 。

问题是,当尝试对我们的代码进行单元测试时,NUnit 或 mstest 未在 AutoCAD 上下文中运行,并且它们也无法启动它。 有一个名为 Gallio 的工具,它提供了与 AutoCAD 的接口,以便可以通过带有命名管道的 IPC 来运行单元测试。

然而,这个解决方案对我来说太麻烦了。 我希望能够快速编写测试,而不必离开我心爱的 IDE。

那么,从“良好的设计角度”来看,什么是解决这个问题的好方法呢? 我想我基本上需要一个不引用 AutoCAD dll 的可测试代码库和一个引用不可测试的 AutoCAD dll 的不可测试代码库。

我确信有一些方法可以让它发挥作用:(IOC、DI、适配器模式……)我只是没有深入了解这些原则,因此我不知道哪条路线最适合我的目的和目标。

We are developing applications for use within AutoCAD.
Basically we create a Class Library Project, and load the .dll in AutoCAD with a command (NETLOAD).

As so, we can use commands, "palettes", user controls, forms etc...

AutoDesk provides an API through some dll's, running in their program directory.
When referencing these dll's you can only call the dll's at runtime while loading your app in AutoCAD (This is a licensing security from AutoDesk).

For us, while developing, this is not a problem, we need to visually test within the context of AutoCAD, so we just set the Debug Properties so that they start acad.exe and load our dll with a script in the acad.exe parameters.

The problem is, when trying to unit test our code, NUnit or mstest are not running from within the AutoCAD context and they also cannot start it.
There exist a tool called Gallio, which has provided an interface with AutoCAD, so that it can run Unit test through IPC with Named Pipes.

However, this solution is, for me, too much of a hassle. I want to be able to quickly write tests without having to leave my beloved IDE.

So, what, from a "good design view" would be a good approach to this problem? I'm thinking I would basically need a testable codebase which is not referencing the AutoCAD dll's and a non-testable that does references the untestable AutoCAD dll's.

I'm sure there are ways to get this to work: ( IOC, DI, Adapter Pattern,. .) I just don't these principles in depth and thus I don't know which route will best suit my purposes and goals.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梨涡 2024-07-20 21:46:28

第一步是将代码分类为需要 AutoCAD 的部分和真正独立的部分。 像平常一样为独立部分创建单元测试。

对于其他部分,您需要行为类似于 AutoCAD 的模型。 使它们尽可能简单(例如,仅在方法中返回正确答案而不进行任何计算)。 现在,您需要几组类:

  1. 您的代码用于实现某些功能(例如,加载绘图)的一组接口。

  2. 调用 AutoCAD dll 的所述接口集的一组实现。

  3. 一组尝试在 AutoCAD 上下文中实现的类。 只需创建一个带有几个按钮的小型 UI,即可在其中运行此代码。 它用于让您自己放心,您的模型做了正确的事情。 将方法参数和结果记录到某个文件中,以便您可以尝试 AutoCAD 如何响应。 如果模型损坏,您可以使用此代码来验证 AutoCAD 正在做什么,并且可以在开发模型时将其用作参考。

  4. 当您了解 AutoCAD 如何响应后,即可创建模型。 在您的测试中,使用所需的结果(和错误,以便您也可以测试错误处理)创建它们。 因此,当您有 boolean loadDrawing(File filename) 时,创建一个模型,该模型为文件名 exists.dxf 返回 truefalse< /code> 其他任何内容。

  5. 使用工厂或 DI 告诉您的应用程序代码要使用哪个实现。 我倾向于有一个大的全局配置类,其中有很多公共字段,我只是在其中存储要使用的对象。 我可以一开始就设置好,速度很快,也很容易理解。 如果您需要在运行时创建对象,请将工厂放入为您生成对象的配置类中,以便您可以交换它们。

The first step is to triage your code for parts which need AutoCAD and parts which are really independent. Create unit tests for the independent parts as you usually would.

For the other parts, you need mockups which behave like AutoCAD. Make them as simple as possible (for example, just return the correct answers in the methods without doing any calculations). Now, you need several sets of classes:

  1. A set of interfaces which your code uses to achieve something (for example, load a drawing).

  2. A set of implementations for said set of interfaces which call the AutoCAD dlls.

  3. A set of classes which try the implementations within the context of AutoCAD. Just create a small UI with a couple of buttons where you can run this code. It is used to reassure yourself that your mockups do the right thing. Log method parameters and results to some file so you can try how AutoCAD responds. If a mockup breaks, you can use this code to verify what AutoCAD is doing and you can use it as a reference when developing the mockups.

  4. When you know how AutoCAD responds, create the mockups. In your tests, create them with the desired results (and errors, so you can test error handling, too). So when you have boolean loadDrawing(File filename), create a mockup which returns true for the filename exists.dxf and false for anything else.

  5. Use a factory or DI to tell your application code which implementation to use. I tend to have a big global config class with a lot of public fields where I simply store the objects to use. I can set this up in the beginning, it's fast, it's easy to understand. If you need to create objects at runtime, then put factories in the config class which generate the objects for you, so you can swap them out.

遗失的美好 2024-07-20 21:46:28

我写了……后来又破坏了……一个 AutoCAD 测试运行程序。 它位于 https://github.com/CADbloke/CADtest。 如果您对此感兴趣,请推动我,我会更快地修复它。 在解决这个问题之前,我正在等待 NUnit v3 发布。

如果您重置到该存储库中的第三次提交(我认为)并从那里摆弄它,它应该运行。

I wrote ... and later broke ... a Test runner for AutoCAD. It is at https://github.com/CADbloke/CADtest. If you're interested in it nudge me along and I'll fix it faster. I am waiting for NUnit v3 release before I tackle it.

If you reset to the 3rd commit in that repo (I think) and fiddle with it from there it should run.

避讳 2024-07-20 21:46:28

这是有关如何对 AutoCAD 插件进行单元测试的详细指南

要点:

  1. 不要使用 NUnit 3,而使用 Nunit 2.x。 我只是将整个 Nunit 2.x 存储库与我的源代码放在一起并进行编译
  2. 使用 RemoteTestRunner 构建测试,为此我参考 RvtUnit 了解如何执行此操作。 RvtUnit 还包含如何编写的示例
    您在 WPF 中自己的测试选择对话框,这对我来说很方便。
  3. 我的测试基础架构包含 2 个 dll,其中一个是通过 IExtensionApplication 连接到 AutoCAD 的“插件”程序集
    类,其中还包含 CommandMethod,我可以通过它运行代码
    运行 RemoteTestRunner 它将把“meat”程序集作为输入
    论证。
  4. meat 程序集包含所有用 NUnit 2.x 语法编写的测试。
  5. 插件程序集中有两种 CommandMethods,一种用于夜间构建期间的全自动测试运行。 对于这个有
    没有对话框,我只是运行所有测试。 又弹出一个
    选择对话框供我随意运行任何测试。 这很有用
    在开发阶段。
  6. 当我想测试对文件的操作时,我使用将文档侧面加载到 AutoCAD 中的方法。 为此我参考了 CADbloke 的 CADtest
    以及如何做到这一点。
  7. 我使用 accoreconsole.exe 运行我的代码。 重要的是要确保测试基础设施中的所有组件以及
    accoreconsole 调用的程序集不能包含 UI dll,例如
    acmgd、AcWindows、AdWindows 因为它们会导致 accoreconsole
    坠毁。 accoreconsole 触及的所有 dll 只能包含
    accoremgd、acdbmgd、AecBaseMgd、AecDbMgd,它们是非 UI dll。

Here's a detailed guide on how you can do the unit testing for AutoCAD plugins.

The key points:

  1. Don't use NUnit 3, use Nunit 2.x. I just put the whole Nunit 2.x repo together with my source code and compile it
  2. Use RemoteTestRunner to construct the test, for this I refer to RvtUnit on how to do it. RvtUnit also contains examples how to write
    your own test selection dialog in WPF, which is handy for me.
  3. My test infrastructure contains 2 dlls, one is the "plugin" assembly that is hooked to the AutoCAD via IExtensionApplication
    class, and which also contains CommandMethod whereby I run the code to
    run RemoteTestRunner which will take the "meat" assembly as input
    argument.
  4. The meat assembly contains all the tests written in the syntax of NUnit 2.x.
  5. There are two CommandMethods in the plugin assembly, one is for the full automated test run during nightly build. For this one there
    is no dialog box, I just run all the tests. Another pops up a
    selection dialog for me to run any tests at will. This is useful
    during development stage.
  6. I use side loading the document into the AutoCAD when I want to test the operation on a file. For this I refer to CADbloke's CADtest
    on how to do it.
  7. I run my code with accoreconsole.exe. It's important to ensure that all the assemblies in the test infrastructure and also the
    assemblies that accoreconsole called cannot contain UI dlls like
    acmgd, AcWindows, AdWindows because they will cause the accoreconsole
    to crash. All dlls the accoreconsole touches can only contain
    accoremgd, acdbmgd, AecBaseMgd, AecDbMgd, they are non-UI dlls.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文