Visual Studio 2010 and test development

I am taking the first steps in Test Driven Development using Visual Studio. I have some questions on how to implement common classes with VS 2010.

First, let's say I want to implement my own version of ArrayList. I start by creating the following test (I use MSTest in this case):

[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 use the ability of VS 2010 to hit

ctrl +.

and let it implement classes / methods on the go.

  • I am having trouble implementing common classes. For example, when I define the .Add(10) method, VS does not know if I intend to use the general method (as a general class) or the Add(int number) method Add(int number) . Is there any way to distinguish this?
  • The same thing happens with return types. Suppose I implement a MyStack stack, and I want to check if, after I click both the item and pop it, the stack is still empty. We all know that pop should return something, but, as a rule, the code of this test should not care about it. Visual Studio will then think that pop is the void method, which is actually not the one you need. How to deal with this? For each method, I have to start by conducting tests that are "very specific", for example, is it obvious that the method must return something so that I do not get such ambiguity? Even if you do not use the result, should I have something like int popValue = myStack.Pop() ?
  • How do I do tests for general classes? Try only one type type? I use int s since they are easy to use, but should I also test various objects? How do you usually approach this?
  • I see that there is a popular TestDriven tool for .NET. With the release of VS 2010, is it still useful, or many of its features are now part of VS 2010, which makes it useless?
  • Whenever I define a new property in my test code and ask VS to generate this method for me, it generates both getter and setter. If I have something like int val = MyClass.MyProperty , I would like to understand that (at least for now) I just want to define getter.

thanks

+6
unit-testing tdd visual-studio mstest testdriven.net
source share
2 answers

I see that there is a popular TestDriven tool for .NET. With the release of VS 2010, is it still useful, or many of its features are now part of VS 2010, which makes it useless?

It is still useful if you use one of several different module testing modules ( nunit , mbunit , xunit , csunit , etc.).

There are other tools (such as Visual Nunit ) that provide visual studio integration for running unit tests.

+3
source share

In your sample code, why do you have a RemoveAt(T obj) method?

Instead, you can do RemoveAt(int index) and Remove(T obj) . Take a look at the Microsoft APIs (for example, for List <T> ), which see how they set the removal methods for the general collection.

And now for your purposes:

1: What would Add(int number) do? If I understand your intentions correctly, Add(10) can only be indexed as "Add a value of 10 at the end of my collection." If you want to add a value to a specific index, you can (and probably should) call this method Insert: Insert(int index, T value) .

2: of course, Visual Studio will first interpret the method as void, but you can edit it as something like

 public class MyStack<T> { public T Pop() { } } 

Pins created by pressing Ctrl+. are convenience, but not the gospel. You should not always assign the return value to a variable. If you do not need it in the test, do not do it. If you want VS to choose a return type other than void, you can write a different unit test (for example, that Pop () returns the last value clicked).

3: I would test the types that I see most commonly used in my code. If you are writing a public API, then check as many types as possible. If you are using NUnit, examine the [TestCase] attribute to avoid code duplication.

4: I still use TestDriven, but I have not tried working without it, so I cannot make a useful comparison.

5: Just uninstall the installer if you do not need it. Some add-ons such as ReSharper support more advanced code generation, including read-only properties.

+3
source share

All Articles