您在单元测试中使用 TestContext 吗?
我想知道,Visual Studio 使用 TestContext
成员生成测试类,并将其实例传递给 ClassInitialize
方法。但说实话,你用过这个类吗?如果是这样,有哪些方便/需要使用它的好例子?
我编写测试已经有一段时间了,但从未真正觉得我需要使用 TestContext
。你呢?
I wonder, visual studio generates test classes with TestContext
member as well as instance of it is passed to ClassInitialize
method. But really, do you use this class? If so, what are the good examples where it's convenient/required to use it?
I have been writing tests for a while and never actually felt I needed to use TestContext
. What about you?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TestContext
通常用于提供附加上下文,例如所需的路径信息、Web 服务、Page
对象或某些数据访问方式。在我看来,如果测试需要这个,我们根本就不是在谈论单元测试。因此,如果您找不到在单元测试中使用 TestContext 的理由,我想您做得很好。
当涉及到集成测试时,这会非常方便。
TestContext
is normally used to provide an additional context, like required path information, web services, thePage
object or some means of data access.In my opinion, if a test needs this, we're not talking about unit tests at all. So if you don't find a reason to use
TestContext
in your unit tests, you're doing just fine I suppose.When it comes to integration tests, this can come in very handy though.
好吧,一方面,我使用它来获取测试输入/输出目录的位置以及加载/保存测试所需或生成的文件。除其他外,还可以打印测试结果消息。
我想完全可以在不使用上下文的情况下编写单元测试,但我从未在重要的测试场景中遇到过这种情况。
Well, for one I use it to obtain the locations of the test input/output directories and load/save files needed or generated by tests. Also to print messages to the test results, among other things.
I suppose it's possible to write unit tests without using the context at all, but I've never encountered that situation in a non-trivial testing scenario.