A Glimpse into TDD - Cycle 1

A Glimpse into TDD – Cycle 1

 

The first step to automation is programming, and the first step to write good code, is to write the unit test first. TDD has been long preached and practiced by various programmers, and in many organizations. But what exactly is TDD, is it just a diagram of Pass, Fail and Refactor, as so many of us know or is it more than that?


Learning TDD is hard, because programming itself is hard. And the shift of the perspective to write a unit test to make the code fail I believe one of the toughest shift in perspective a developer goes through. And this shift is crucial not with the developer mindset in your team, but in the management as well. Martin Fowler, in this insightful article describes about it, and I thought sharing of link will be better than trying to use my words, so here it is - https://martinfowler.com/bliki/TestPyramid.html

Another term which popped up while I was glimpsing into the world of TDD was Code Kata, picking the definition directly from Wikipedia –“A code kata is an exercise in programming which helps programmers hone their skills through practice and repetition”. And there are a lot of resources out there on this topic as well, but maybe in the coming days I will delve more deeper into this than just a glimpse.

 

 

 

The reason I introduced this term was to talk about a problem used in explaining TDD to beginners like I am here – FizzBuzz. Picking its definition from Wikipedia again in this problem we basically teach kids to say Fizz, if we say 3, say Buzz, if we say 5, say Fizz Buzz if we say a number which can be divided by both 3 and 5, and for any other number, say the number back.

 

So if I say 10, you will say 10, if I say 9, you will say Fizz, if I say 21 you will say Fizz again only to say Buzz, if I say 25, and if I say 30, Fizz Buzz and I hope you understood how this whole thing works. I am definitely going to try this with my son, for his division practice but for now, in this article I am sharing a small C# TDD implementation which I used to solve this problem.

This is just my cycle 1, a lot of code refactoring is needed, and I am sure I am defied one principle or the other, but I just glimpsed this today. So here’s my humble attempt and this cycle is currently under progress, not to mention my monthly cycle is about to end today! Phew! Okay so here it goes –

 

 

 

 

 

 

Technical Stack – C# as programming language, xUnit nuget package, FluentAssertions nuget package.

FizzBuzzTest [xUnitTest]

using FizzBuzz;

using System;

using Xunit;

using FluentAssertions;

 

namespace FizzBuzz.Tests

{

    public class FizzBuzzTests

    {

        public void test()

        {

           

        }

 

        [Theory]

        [InlineData(2)]

        public void shouldProcessInput(int num)

        {

            //Arrange

            FizzBuzzClass fbObj = new FizzBuzzClass();

            //Act

            //var result = fbObj.process(num);

            // Assert

            fbObj.process(num).Should().Be("2");

           

        }

 

        [Theory]

        [InlineData(3)]

        public void shouldReturnFizz(int num)

        {

            //Arrange

            FizzBuzzClass fbObj = new FizzBuzzClass();

            //Act

            //var result = fbObj.process(num);

            // Assert

            fbObj.process(num).Should().Be("Fizz");

 

        }

        [Theory]

        [InlineData(5)]

        public void shouldReturnBuzz(int num)

        {

            //Arrange

            FizzBuzzClass fbObj = new FizzBuzzClass();

            //Act

            //var result = fbObj.process(num);

            // Assert

            fbObj.process(num).Should().Be("Buzz");

 

        }

 

        [Theory]

        [InlineData(15)]

        public void shouldReturnFizzBuzz(int num)

        {

            //Arrange

            FizzBuzzClass fbObj = new FizzBuzzClass();

            //Act

            //var result = fbObj.process(num);

            // Assert

            fbObj.process(num).Should().Be("FizzBuzz");

 

        }

    }

}

FizzBuzzClass  [class library]

using System;

 

namespace FizzBuzz

{

    public class FizzBuzzClass

    {

        public void execute()

        {

 

        }

        public string process(int num)

        {

            if (num == 3)

            {

                return "Fizz";

            }

            if (num == 5)

            {

                return "Buzz";

 

            }

            if((num%3==0) && (num % 5 == 0))

            {

                return "FizzBuzz";

            }

            return num.ToString();

        }

 

    }

}

 

I have to admit that the urge to keep writing my code, only to fail and refactor is kind of unstoppable but I have other tasks to attend to. So for now I leave this here only to visit it later, as there is a lot of information out there still I need to read, access, and then put into implementation. Hopefully until the coming session of The Glimpse I will have more to talk about.

Until then folks!

disclaimer- I have used a lot of references/links to write this article and here's the current list as of now -

https://www.youtube.com/watch?v=y8TcPr73Bwo https://www.ibm.com/garage/method/practices/code/practice_test_driven_development/
https://www.ibm.com/cloud/architecture/content/course/test-driven-development

https://technologyconversations.com/2013/12/20/test-driven-development-tdd-example-walkthrough/

https://dev.to/napicella/test-driven-development-by-example-29g8

https://learnitmyway.com/tdd-example-java/

https://learnitmyway.com/tdd-example/

https://en.wikipedia.org/wiki/Kata_(programming)

http://codekata.com/

https://codingdojo.org/KataCatalogue/

https://www.codewars.com/