一次性初始化NUnit的 [英] One Time initialization for Nunit

查看:351
本文介绍了一次性初始化NUnit的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该在哪里放置的代码,应该只(每类,而不是一次)运行一次?一个这样的例子是初始化数据库连接字符串的声明。而我只需要运行一次,我不想将每个的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提供安装和拆卸
整个组装

A SetUpFixture outside of any namespace provides SetUp and TearDown for the entire assembly.

所以如果你需要设置 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的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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