My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
Testing With TDD And BDD

Testing With TDD And BDD

Milecia's photo
Milecia
·May 2, 2019

Testing is an un-skippable part of any development process (even if it does get skipped in practice). There are different methodologies you can use for your testing. The two big methodologies being used right now are test driven development (TDD) and behavior driven development (BDD).

They both have their own ways of handling testing and they both work best when used together. That's why it is a good idea to know about both so that you can choose what works for your projects.

TDD

Test-driven development is a process focused on writing tests. You write tests before you write any code. Then you check to see that the test fails, which you would expect since there is no code implemented yet. Next you'll write the minimum amount of code needed to pass the test. Once your code is passing the test, you can work on refactoring it to make it more efficient.

You go through this process for every piece of code you write. That means you'll end up with a lot fewer bugs, it'll be easier to add new features, and you have a final check of any merged code before you deploy to a production server. That is the heart of TDD.

It's hard for many developers to use TDD in the beginning because it feels strange writing your tests before you have any code to test. This is a skill that you really have to work at building but once you get it, the pay off is incredible. The reliability of your code skyrockets, you don't have to depend on outside tools for your testing, and your overall programming skills start to improve.

BDD

Behavior-driven development is completely different from test driven development. BDD is actually a set of best practices for writing really good tests. One of the most important issues BDD addresses is how you do the implementation for tests.

With BDD, you focus on writing tests to check scenarios or behaviors instead of checking the implementation of your code. Most of the time you'll see tests in BDD written like: "it should do something". An example of this is: "should create user id after calling save". One advantage of writing tests this way is that you won't have to update the test implementation every time you update the code implementation.

That's one of the biggest complaints I've heard from developers about testing. BDD fixes that. Since you're testing behavior instead of implementation, the code implementation doesn't impact the test implementation.

How they complement each other

Test-driven development and behavior-driven development complement each other. TDD makes sure that you write tests in the first place and BDD tells you how you should be writing those tests. Then you have a well-defined testing process that can be applied to any project, regardless of the programming language(s).

Behavior-driven development forces you to think about every step a user will go through. This really helps you learn how to figure out what a client or a manager really wants you to do because you start thinking about business cases.

Test-driven development forces you to write tests for each of those behaviors. When you know the exact behavior a user is expecting, it makes it easier to write the code for it. It makes you write more efficient code because you are only writing enough to pass the test and that test maps directly to a user process.

By combining both methodologies, you get the most benefit from your testing efforts. Then you can start getting into automated tests which makes things even easier when all of the tests have been written in a useful way.

Are there any other testing methodologies you're familiar with? These are just the ones I've worked with so I'm curious to hear about any others out there. 🙂


You can stay in the loop with what's happening in the web development world when you sign up for my emails. You can click here to do that. There's hundreds of people who already get the updates, so why not you too?