Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 802949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:40:49+00:00 2026-05-14T23:40:49+00:00

I am doing my first steps with TDD. The problem is (as probably with

  • 0

I am doing my first steps with TDD. The problem is (as probably with everyone starting with TDD), I never know very well what kind of unit tests to do when I start working in my projects.

Let’s assume I want to write a Stack class with the following methods(I choose it as it’s an easy example):

Stack<T>
 - Push(element : T)
 - Pop() : T
 - Peek() : T
 - Count : int
 - IsEmpty : boolean

How would you approch this? I never understood if the idea is to test a few corner cases for each method of the Stack class or start by doing a few “use cases” with the class, like adding 10 elements and removing them. What is the idea? To make code that uses the Stack as close as possible to what I’ll use in my real code? Or just make simple “add one element” unit tests where I test if IsEmpty and Count were changed by adding that element?

How am I supposed to start with this?

EDIT

Here’s my rough tests’ implementation:

    [TestMethod]
    public void PushTests() {
        StackZ<string> stackz = new StackZ<string>();

        for (int i = 0; i < 5; ++i) {
            int oldSize = stackz.Size;
            stackz.Push(i.ToString());
            int newSize = stackz.Size;
            Assert.AreEqual(oldSize + 1, newSize);
            Assert.IsFalse(stackz.IsEmpty);
        }
    }

    [TestMethod, ExpectedException(typeof(InvalidOperationException))]
    public void PeekTestsWhenEmpty() {
        StackZ<double> stackz = new StackZ<double>();
        stackz.Peek();
    }

    [TestMethod]
    public void PeekTestsWhenNotEmpty() {
        StackZ<int> stackz = new StackZ<int>();
        stackz.Push(5);

        int firstPeekValue = stackz.Peek();

        for (int i = 0; i < 5; ++i) {
            Assert.AreEqual(stackz.Peek(), firstPeekValue);
        }
    }

    [TestMethod, ExpectedException(typeof(InvalidOperationException))]
    public void PopTestsWhenEmpty() {
        StackZ<float> stackz = new StackZ<float>();
        stackz.Pop();
    }

    [TestMethod]
    public void PopTestsWhenNotEmpty() {
        StackZ<int> stackz = new StackZ<int>();

        for (int i = 0; i < 5; ++i) {
            stackz.Push(i);
        }

        for (int i = 4; i >= 0; ++i) {
            int oldSize = stackz.Size;
            int popValue = stackz.Pop();
            Assert.AreEqual(popValue, i);
            int newSize = stackz.Size;
            Assert.AreEqual(oldSize, newSize + 1);
        }

        Assert.IsTrue(stackz.IsEmpty);
    }

Any corrections/ideas about it? Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:40:50+00:00Added an answer on May 14, 2026 at 11:40 pm

    Start by testing the basic principles of your API.

    Test on zero elements.

    • Test that it’s empty.
    • Count is zero.
    • Pop fails.

    Test on one element:

    • Call Push.
    • Test that it’s not empty.
    • Test that count is 1.
    • Test that Pop returns the element.
    • Test that it’s now empty.
    • Test that count is now 0.

    Test on >1 elements:

    • Now Push 2 and test count is two.
    • Pop 2 and make sure they come in LIFO order.
    • Check the emptiness and counts.

    Each of these would be at least one test case.

    For example (roughly outlined in Google’s unit test framework for c++):

    TEST(StackTest, TestEmpty) {
      Stack s;
      EXPECT_TRUE(s.empty());
      s.push(1);
      EXPECT_FALSE(s.empty());
      s.pop();
      EXPECT_TRUE(s.empty());
    }
    
    TEST(StackTest, TestCount) {
      Stack s;
      EXPECT_EQ(0, s.count());
      s.push(1);
      EXPECT_EQ(1, s.count());
      s.push(2);
      EXPECT_EQ(2, s.count());
      s.pop();
      EXPECT_EQ(1, s.count());
      s.pop();
      EXPECT_EQ(0, s.count());
    }
    
    TEST(StackTest, TestOneElement) {
      Stack s;
      s.push(1);
      EXPECT_EQ(1, s.pop());
    }
    
    TEST(StackTest, TestTwoElementsAreLifo) {
      Stack s;
      s.push(1);
      s.push(2);
      EXPECT_EQ(2, s.pop());
      EXPECT_EQ(1, s.pop());
    }
    
    TEST(StackTest, TestEmptyPop) {
      Stack s;
      EXPECT_EQ(NULL, s.pop());
    }
    
    
    TEST(StackTest, TestEmptyOnEmptyPop) {
     Stack s;
      EXPECT_TRUE(s.empty());
      s.pop();
      EXPECT_TRUE(s.empty());
    }
    
    TEST(StackTest, TestCountOnEmptyPop) {
      Stack s;
      EXPECT_EQ(0, s.count());
      s.pop();
      EXPECT_EQ(0, s.count());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.