在运行的xUnit的装配载码 [英] Running code on assembly load in xUnit

查看:197
本文介绍了在运行的xUnit的装配载码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是努力库作为模拟数据库,它有一个具有运行一些初始化代码在装配负载。

I am using the Effort library as a mock database, and it has some initialization code that has to run on assembly load.

我曾尝试添加在静态构造函数这个初始化代码是从每个测试通过基类的构造函数引用,但是这并没有满足要求库。

I have tried adding this initialization code in a static constructor that is referenced from every test via a base class constructor but this did not meet the requirements of the library.

这是回答类似的问题的这里提到的 AssemblyInitialize 属性,但这并不适用于的xUnit - 的xUnit没有上运行的代码的方式装配载荷。

An answer to a similar question here mentions an AssemblyInitialize attribute, but this is not applicable to xUnit - xUnit has no way of running code on assembly load.

我也问在 GitHub上的xUnit页面 - ?有没有明确汇编级初始化

I also asked on the xUnit GitHub pages - there is definitively no assembly level initializer.

我如何运行,我需要在xUnit的初始化代码

How can I run the initialization code that I need to in xUnit?

推荐答案

我回答我的问题,因为我一直在试图解决这出星期,终于发现,有效的解决方案。

I'm answering my own question because I've been trying to sort this out for weeks and have finally found a solution that works.

这就是所谓的第三方独立包模块初始化程序 。有一个的NuGet 封装,使用简单:在汇编安装包,安装脚本将创建一个名为 ModulerInitializer.cs 在您的项目。它还将增加一个生成后步骤到项目文件将在注入中的代码ModuleInitializer.Run()插入模块初始化。这是保证之前在模块运行的任何其他代码运行,在任何类型的初始化器,静态构造或任何其它初始化代码

It's a third-party standalone package called Module Initializer. There's a NuGet package that's simple to use: install the package in your assembly, and the install script will create a file called ModulerInitializer.cs in your project. It will also add a post-build step to the project file which will inject the code in ModuleInitializer.Run() into the module initializer. This is "guaranteed to run before any other code in the module runs, before any type initializers, static constructors or any other initialization code".

在实践中,这意味着所有你所要做的就是安装包和写入设置代码 - 它真的不能更简单。 。荣誉属于所有参与

In practice this means that all you have to do is install the package and write the setup code - it really couldn't be more straightforward. Kudos to all involved.

有也被称为一种很有前途的前瞻性库的 Fody.ModuleInit 。然而,这是一个更复杂的装配编织库的插件 - 在我的情况下,简单的解决方案是比较合适的。

There is also a promising-looking library called Fody.ModuleInit. However, this is a plugin for a more complex assembly weaving library - in my case the simpler solution was more appropriate.

这篇关于在运行的xUnit的装配载码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆