Test 1 Create a Stack and verify that Is Empty is true

This test requires creating a Stack and then calling the IsEmpty property. The IsEmpty property should return true because we haven't put any elements into the Stack. Let's create a file called StackFixture.cs, in which we write a test fixture class, called StackFixture, to hold the tests.

using System;

using NUnit.Framework;


There are a few things of ¡Merest about this class. The line using NUnit.Framework; is needed to reference the custom attributes defined in NUnit th at are uhed to mark the test fixture. The [TestFixture] attribute can be associated only with a class. This attribute is an indicator to NUnit: that the class contains test methods.

The next activity) if to write the method that does the test. (Here, the test method name is Empty.)

public void Empty() {

Stack stack = new Stack(); Assert.IsTrue(stack.IsEmpty) ;

The test method is marked with the attribute [Test]. The first thing is to create a Stack object. After creating the object, we use the Assert.IsTrue(...) method to verify that the return value of IsEmpty is true.

Although the class used in the test, Stack, and the property IsEmpty do not exist, we are writing test code as if they do. We are thinking about how the class and its methods are used instead of how to implement it, which is an important distinction. This is why many people refer to test-driven development as much a design technique as a testing technique. Many times, class library designers implement a library and then figure out how to use it, which can lead to libraries that require a lot of initialization, complex method interactions, and increased dependencies. Thinking about how to use the library before implementing it places a greater emphasis on usage, which often leads to better design.

Because the Stack class does not exist, the test does not compile. Thafs easy enough to fix. What is the smallest amount of work that needs to be done to get this to compile?

using System;

public class Stack {

private bool isEmpty = true;

public bool IsEmpty {

return isEmpty;

This implementation is certainly small; in fact, it might seem surprising. Remember that the goal is to do the smallest amount of work possible to get the code to compile. Some people might say that this code is too complicated: given the test, they might argue that the following code would be sufficient:

public bool IsEmpty {

return true;

There is a balance to achieve between anticipating future tests and implementation and being totally ignorant of the next test. In the beginning, you should focus on the test you are writing and not think about the other tests. As you become familiar with the technique and the task, you can increase the size of the steps. You should always keep in mind that large steps are harder to debug than smaller steps. Also, if your code is too complicated or provides functionality that is not tested, additional refactorings can result later.

This discussion is also relevant to the earlier discussion about the test list. It is very clear from the test list that you have to store multiple items. Should you go ahead and use an ArrayList because you might need it later? No—the current tests do not support the need for an ArrayList. Wait and see what the tests look like before making that decision.

Now that the code compiles, it is time to run the test in NUnit. The green bar displays, which indicates success. We can check off the first test and move on.

Which test should we chpose next? Perhaps we should stay focused on the IsEmpty property because it is probably rhe smallest increment over what we have now. Let's look at "Push a single object on the Stack and verify that IsEmpty is false."

0 0


  • susanna v
    How create a stack and verify that isempty is true in dotnet?
    7 years ago

Post a comment