MSTest每次启动时可以运行特定的方法吗?

发布于 2024-07-08 10:49:22 字数 588 浏览 6 评论 0原文

问题

有没有办法让一个方法在通过 MSTest 运行测试程序集时始终运行?

与 [TestInitialize] 和 [ClassInitialize] 属性的工作方式类似,但适用于整个程序集。 我不想向每个测试类的 [ClassInitialize] 方法添加代码。

推理

我的一些测试与数据库交互。 他们删除对生产数据库非常有害的数据和其他内容。 只有一个配置文件告诉我的单元测试项目针对非生产数据库运行。

如果有一种在启动时运行的方法会说“好吧,数据库名称不是‘生产’”,我会感觉更好。

Ideas

Log4Net 使用程序集属性来配置自身。

using log4net.Config;
[assembly: XmlConfigurator()]

也许我可以做一些类似的事情?

[assembly:  CheckDatabaseNameNot("production")]

Question

Is there a way to have a method that will always run anytime that test assembly is run through MSTest?

Similar to how the [TestInitialize] and [ClassInitialize] attributes work, but for the entire assembly. I do not want to have to add code to every test class's [ClassInitialize] method.

Reasoning

Some of my tests interact with the database. They delete data and other things that would be very harmful to a production database. There is only a configuration file that tells my unit test project to run against the non-production database.

I would feel better if there was a method that would run on startup that would say "Okay Database name is not 'production'"

Ideas

Log4Net uses an assembly attribute to configure itself.

using log4net.Config;
[assembly: XmlConfigurator()]

Perhaps I can do something simliar?

[assembly:  CheckDatabaseNameNot("production")]

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

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

发布评论

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

评论(1

红衣飘飘貌似仙 2024-07-15 10:49:22

您尝试过[AssemblyInitialize]吗?

Have you tried [AssemblyInitialize]?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文