NUnit的一次性初始化 [英] One-time initialization for NUnit

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

问题描述

我应该在哪里放置只能运行一次(而不是每个类一次)的代码?

此示例为初始化数据库连接字符串的语句.而且我只需要运行一次,并且我不想在每个"TestFixture"窗口中放置一个新方法.上课只是为了做到这一点.

解决方案

[SetUpFixture] 属性可让您在同一名称空间下为所有测试运行一次安装和/或拆卸代码.

此处 SetUpFixture 上的文档.根据文档:

任何名称空间之外的SetUpFixture都为整个程序集提供SetUp和TearDown.

因此,如果所有测试都需要 SetUp TearDown ,则只需确保 SetUpFixture 类不在命名空间中.

或者,您始终可以严格地定义静态类,以定义全局"测试变量.

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 database 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.

解决方案

The [SetUpFixture] attribute allows you to run setup and/or teardown code once for all tests under the same namespace.

Here is the documentation on SetUpFixture. According to the documentation:

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

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天全站免登陆