重复通用代码的C#单元测试 [英] c# unit test with common code repeated

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

问题描述

可能重复:
可以替代[SetUp]和在MSTest中[TearDown]?

Possible Duplicate:
What would be an alternate to [SetUp] and [TearDown] in MSTest?

我正在学习一般如何使用单元测试和自动化测试,并且有一些我无法弄清/找到答案的问题

I'm learning how to use Unit Testing and automated testing in general and have a few questions I can't figure out/find the answer to

我目前通过多个[TestMethod]进行测试,这些[TestMethod]调用各种方法并在其中包含断言.现在,TestMethod的所有程序都有重复的代码来命中数据库,并为其余的测试进行自我设置.一个例子是:

I currently test by having multiple [TestMethod]s that call various methods and have asserts in them. Right now the TestMethod's all have duplicate code to hit a DB and set themself up for the rest of the test. An example is:

public void TestDBReturnsFooInFormatXyz() {
    var foo = HitDBAndReturnStuff();
    Assert.IsTrue( // foo format is xyz );
}

public void TestDBFooContainsAbc() {
    var foo = HitDBAndReturnStuff();
    Assert.IsTrue( // foo contains abc );
}

一些问题: 最好的方法是在测试类中创建私有字段,并让构造函数对其进行设置吗?

So some questions: Is it a best practice to make private fields in a test class and have the constructor set them?

我应该在每个TestMethod中这样做,因为测试速度没那么重要吗?

Should I do it in each TestMethod because testing speed doesn't matter that much?

我应该在构造函数上放什么[Test ???],以确保在运行测试时调用它?

What [Test???] do I put on top of the constructor to make sure it gets called when running tests?

我看过MSDN和《编程Microsoft Visual C#2008:语言》一书,发现关于单元测试没有任何好的信息.如果有我应该阅读的资源,可以回答这些问题,请告诉我.

I've looked at MSDN and the book "Programming Microsoft Visual C# 2008: The Language" and I can't find any good information on unit testing. If there is a resource I should have read which answers these questions just let me know.

谢谢!

推荐答案

重复的代码始终是一种不好的做法,因此您应重构代码以初始化foo 对象. http://msdn.microsoft.com/zh-CN/library/microsoft.visualstudio.testtools.unittesting.testinitializeattribute%28v=vs.80%29.aspx"rel =" nofollow noreferrer> TestInitialize方法,该操作在每次测试运行之前执行一次,然后在每个测试用例中执行特定的操作:

Duplicated code is always a bad practice, so you should refactor your code to initialize the foo object in the TestInitialize Method, which executes once before each test run, and then perform specific operations in each test case:

private FooType foo;

[TestInitialize()]
public void TestInitialize()
{
    foo = CreateFooObject();
}

[TestMethod()]
public void TestToAssertThatAbcStuffGetsDone()
{
    foo.DoAbcStuff();
    Assert.IsTrue(foo.DidAbc());
}

[TestMethod()]
public void TestToAssertThatXyzStuffGetsDone()
{
    foo.DoXyzStuff();
    Assert.IsTrue(foo.DidXyz());
}

如果在每次运行测试方法后都需要处置 foo 对象,则还应该实现

If your foo object needs to be disposed after each test method run, you should also implement a TestCleanup Method.

但是,

However, using a database in your unit tests is not considered a best practice because you don't want external dependencies affecting your tests results. Instead, you should use a database mock that acts like a database but isolates your unit tests from the real database dependency.

几个与数据库有关的问题可能会破坏您的测试,例如连接失败,超时,数据更改和偶尔的数据库升级. 此SO问题讨论了处理数据库依赖问题.

Several database related problems could break your tests, such as connection failures, timeouts, data changes and occasional database upgrades. This SO question discusses strategies for handling the database dependency issue.

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

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