Visual Studio 2010 和测试驱动开发
我正在使用 Visual Studio 迈出测试驱动开发的第一步。我有一些关于如何使用 VS 2010 实现泛型类的问题。
首先,假设我想实现我自己的 ArrayList 版本。 我首先创建以下测试(在本例中我使用 MSTest):
[TestMethod]
public void Add_10_Items_Remove_10_Items_Check_Size_Is_Zero() {
var myArrayList = new MyArrayList<int>();
for (int i = 0; i < 10; ++i) {
myArrayList.Add(i);
}
for (int i = 0; i < 10; ++i) {
myArrayList.RemoveAt(0); //should this mean RemoveAt(int) or RemoveAt(T)?
//VS doesn't know. Any work arounds?
}
int expected = 0;
int actual = myArrayList.Size;
Assert.AreEqual(expected, actual);
}
我使用 VS 2010 的能力来命中
ctrl + .
并让它随时随地实现类/方法。
- 我在实现泛型类时遇到了一些麻烦。例如,当我定义一个
.Add(10)
方法时,VS 不知道我是想要一个通用方法(因为该类是通用的)还是一个Add(int number)< /代码> 方法。有什么方法可以区分吗?
- 返回类型也会发生同样的情况。假设我正在实现一个
MyStack
堆栈,并且我想测试在压入元素并弹出它之后,堆栈是否仍然为空。我们都知道 pop 应该返回一些东西,但通常,这个测试的代码不应该关心它。 Visual Studio 会认为 pop 是一种 void 方法,这实际上不是人们想要的。这该如何处理呢?对于每种方法,我是否应该首先进行“非常具体”的测试,例如显然该方法应该返回一些内容,这样我就不会出现这种歧义?即使不使用结果,我是否应该有类似int popValue = myStack.Pop()
的东西? - 我应该如何对泛型类进行测试?仅使用一种通用类型进行测试?我一直在使用
int
,因为它们很容易使用,但我还应该使用不同类型的对象进行测试吗?您通常如何处理这个问题? - 我看到有一个流行的工具,名为 TestDriven for .NET。随着 VS 2010 的发布,它是否仍然有用,或者它的很多功能现在已经成为 VS 2010 的一部分,使其变得有点无用?
- 每当我在测试代码中定义一个新属性,并要求 VS 为我生成该方法存根时,它都会生成一个 getter 和一个 setter。如果我有类似 int val = MyClass.MyProperty 的内容,我想了解(至少到目前为止)我只想定义一个吸气剂。
谢谢
I'm making my first steps in Test Driven Development with Visual Studio. I have some questions regarding how to implement generic classes with VS 2010.
First, let's say I want to implement my own version of an ArrayList.
I start by creating the following test (I'm using in this case MSTest):
[TestMethod]
public void Add_10_Items_Remove_10_Items_Check_Size_Is_Zero() {
var myArrayList = new MyArrayList<int>();
for (int i = 0; i < 10; ++i) {
myArrayList.Add(i);
}
for (int i = 0; i < 10; ++i) {
myArrayList.RemoveAt(0); //should this mean RemoveAt(int) or RemoveAt(T)?
//VS doesn't know. Any work arounds?
}
int expected = 0;
int actual = myArrayList.Size;
Assert.AreEqual(expected, actual);
}
I'm using VS 2010 ability to hit
ctrl + .
and have it implement classes/methods on the go.
- I have been getting some trouble when implementing generic classes. For example, when I define an
.Add(10)
method, VS doesn't know if I intend a generic method(as the class is generic) or anAdd(int number)
method. Is there any way to differentiate this? - The same can happen with return types. Let's assume I'm implementing a
MyStack
stack and I want to test if after I push and element and pop it, the stack is still empty. We all know pop should return something, but usually, the code of this test shouldn't care for it. Visual Studio would then think that pop is a void method, which in fact is not what one would want. How to deal with this? For each method, should I start by making tests that are "very specific" such as is obvious the method should return something so I don't get this kind of ambiguity? Even if not using the result, should I have something likeint popValue = myStack.Pop()
? - How should I do tests to generic classes? Only test with one generic kind of type? I have been using
int
s, as they are easy to use, but should I also test with different kinds of objects? How do you usually approach this? - I see there is a popular tool called TestDriven for .NET. With VS 2010 release, is it still useful, or a lot of its features are now part of VS 2010, rendering it kinda useless?
- Whenever I define a new property in my test code, and ask VS to generate that method stub for me, it generates both a getter and a setter. If I have something like
int val = MyClass.MyProperty
i'd like to to understand that (at least yet) I only want to define a getter.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用多种不同的单元测试框架之一(nunit,< a href="http://www.mbunit.com/" rel="nofollow noreferrer">mbunit, xunit、csunit 等)。
还有其他工具(例如 Visual Nunit) 提供用于运行单元测试的 Visual Studio 集成。
It's still useful in case you use one of a number of different unit testing frameworks (nunit, mbunit, xunit, csunit, etc).
There are also other tools (like Visual Nunit) that provide visual studio integration for running unit tests.
对于您的代码示例,为什么会有一个方法
RemoveAt(T obj)
?您可以改为执行 ),了解他们如何为通用集合设置删除方法。
RemoveAt(int index)
和Remove(T obj)
。查看 Microsoft 的 API(例如,列出现在来说说你的观点:
1:
Add(int number)
会做什么?如果我正确理解你的意图,Add(10)
只能解释为“在我的集合末尾添加值10”。如果您想在特定索引处添加值,您可以(并且可能应该)将该方法命名为 Insert:Insert(int index, T value)
。2:当然,Visual Studio 首先会将该方法解释为 void,但您可以将其编辑为类似
通过按
Ctrl+.
构建的存根(stub),这很方便,但不是福音。您不必总是为变量分配返回值。如果您在测试中不需要它,请不要这样做。如果您希望 VS 获取除 void 之外的返回类型,您可以编写不同的单元测试(例如 Pop() 返回最后推送的值)。3:我会使用代码中最常用的类型进行测试。如果您正在编写公共 API,请使用尽可能多的类型进行测试。如果您使用的是 NUnit,请考虑使用
[TestCase]
属性来帮助您避免编写一些重复的代码。4:我仍然使用 TestDriven,但我还没有尝试过不使用它,所以我无法真正进行有用的比较。
5:不需要的话直接删除setter即可。一些插件框架(例如 ReSharper)支持更高级的代码生成,包括只读属性。
To your code sample, why would you have a method
RemoveAt(T obj)
?You can do
RemoveAt(int index)
andRemove(T obj)
instead. Take a look at Microsoft's APIs (for example, for List<T>) that see how they set up the Remove methods for a generic collection.And now for your points:
1: What would
Add(int number)
do? If I understand your intentions correctly,Add(10)
can only be intepreted as "Add value 10 at the end of my collection". If you wanted to add a value at a particular index, you can (and probably should) name that method Insert:Insert(int index, T value)
.2: sure, Visual Studio will interpret the method as void at first, but you can edit it to be something like
The stubs built by pressing
Ctrl+.
are a convenience, but not gospel. You don't HAVE to always assign a return value to a variable. If you don't need it in a test, don't do it. If you want VS to pick up on a return type other than void, you can write a different unit test (e.g. that Pop() returns the last pushed value).3: I'd test with the types that I see most frequently used in my code. If you're writing a public API, then test with as many types as possible. If you're using NUnit, look into using the
[TestCase]
attribute to help you avoid writing some duplicate code.4: I still use TestDriven, but I haven't tried going without it, so I can't really make a useful comparison.
5: Just delete the setter if you don't need it. Some addon frameworks like ReSharper support more advanced code generation, including read-only properties.