重复通用代码的C#单元测试

编程入门 行业动态 更新时间:2024-10-11 19:25:08
本文介绍了重复通用代码的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 对象. msdn.microsoft/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 对象,则还应该实现 TestCleanup方法.

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#单元测试

本文发布于:2023-11-27 10:55:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637782.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元测试   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!