问题描述
我应该在哪里放置的代码,应该只(每类,而不是一次)运行一次?一个这样的例子是初始化数据库连接字符串的声明。而我只需要运行一次,我不想将每个的TestFixture类中的新方法只是为了做到这一点。
Where should I place code that should only run once (and not once per class)? An example for this would be a statement that initializes the DB connection string. And I only need to run that once and I don't want to place a new method within each "TestFixture" class just to do that.
推荐答案
的 [SetUpFixture]
属性允许您为同一个命名空间下的所有测试运行安装程序和/或拆除码一次。
The [SetUpFixture]
attribute allows you to run setup and/or teardown code once for all tests under the same namespace.
是文档在 SetUpFixture
。根据文档:
Here are the docs on SetUpFixture
. According to the docs:
任何名称空间之外的SetUpFixture提供安装和拆卸
整个组装
所以如果你需要设置
和 TearDown中
所有测试,则只需确保 SetUpFixture
类不在一个命名空间。
So if you need SetUp
and TearDown
for all tests, then just make sure the SetUpFixture
class is not in a namespace.
另外,您总是可以严格定义一个静态类定义为全球测试变量的目的。
Alternatively, you could always define a static class strictly for the purpose of defining "global" test variables.
这篇关于一次性初始化NUnit的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!