C#单元测试代码题 [英] C# unit test code questions

查看:28
本文介绍了C#单元测试代码题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们开始使用 C# 内置单元测试功能.我有 VisualStudio 2008 为我创建了单元测试代码.我在生成的代码上方几乎没有问题.以下是我从生成的文件中复制的代码:

We start use C# build-in unit test functionality. I have VisualStudio 2008 created unit test code for me. I have few question above the generated code. Following are code I copied from the generated file:

  #region Additional test attributes
  // 
  //You can use the following additional attributes as you write your tests:
  //
  //Use ClassInitialize to run code before running the first test in the class
  //[ClassInitialize()]
  //public static void MyClassInitialize(TestContext testContext)
  //{
  //}
  //
  //Use ClassCleanup to run code after all tests in a class have run
  //[ClassCleanup()]
  //public static void MyClassCleanup()
  //{
  //}
  //
  //Use TestInitialize to run code before running each test
  //[TestInitialize()]
  //public void MyTestInitialize()
  //{
  //}
  //
  //Use TestCleanup to run code after each test has run
  //[TestCleanup()]
  //public void MyTestCleanup()
  //{
  //}
  //
  #endregion

如果我需要初始化和清理方法,我是否需要在启用它们时从方法名称中删除那些我的"?

If I need the initialize and cleanup methods, do I need to remove those "My" from the method name when I enable them?

  //Use ClassInitialize to run code before running the first test in the class
  //[ClassInitialize()]
  //public static void MyClassInitialize(TestContext testContext)
  //{
  //}

在运行第一个测试之前,我是否需要在某处调用 "MyClassInitialize" 方法,或者它会在调用其他方法之前自动调用.

Do I need to call the "MyClassInitialize" method somewhere before running the first test or it will be called automatically before other methods are called.

其他三种方法的类似问题,它们是否在正确的时间范围内自动调用?

Similar questions for other three methods, are they called automatically at right time frame?

推荐答案

测试框架会自动调用这些方法.我相信,您可以根据需要重命名它们,因为它使用属性来标识在适当的时间调用的适当方法.请记住取消注释属性以及方法本身,否则测试框架将无法找到该方法并调用它.

The methods are called automatically by the test framework. You can rename them if you want, I believe, as it uses the attributes to identify the proper methods to invoke at the proper time. Remember to uncomment the attributes as well as the method itself or the testing framework won't be able to find the method and invoke it.

这篇关于C#单元测试代码题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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