DEV Community

Discussion on: Explain Unit Testing like I'm five

Collapse
 
hilaberger92 profile image
Hila Berger

Shortly, unit tests are methods that test small pieces of your code, for example, one specific method.

They are written mostly by programmers in order to ensure that their code works before writing the next piece of code.

There are two main approaches for writing unit tests:

BDD - the programmer writes the code and then writes the test.

TDD - the programmer writes the test and then writes the code.

Collapse
 
nestedsoftware profile image
Nested Software

I think this characterization of BDD is not quite right. I believe that BDD also emphasizes testing first, although they use some different terminology (like behaviour vs test) and maybe alter the emphasis of some things.

Collapse
 
hilaberger92 profile image
Hila Berger

Thanks for the comment!
What is the full definition of BDD in your opinion?

Thread Thread
 
nestedsoftware profile image
Nested Software • Edited

Hi Hila, the nice thing about TDD is that it's a clear and well defined concept. You write a test. Test fails. You write the code to pass the test. You refactor to clean things up. You write another test, and so on.

What about BDD? Well, I am not an expert, but I see BDD as kind of a generalization, maybe an extension as well. For one thing, BDD doesn't usually use the word "test" even when talking about what is essentially a unit test. BDD tries to emphasize the desired behaviour of the application, and as such it encompasses both what we'd call "unit" tests, i.e. small-scale tests of logic, as well as maybe "integration" tests, i.e. higher-level functionality of the system as a whole.

I get a sense that some of BDD is not so much about writing code as it is about opening communication channels between developers and people who are driving the requirements, such as users, business people, or managers.

I don't think that BDD is as well-defined as TDD, and as such there is potentially more room for confusion or disagreement.

Some links:

Thread Thread
 
hilaberger92 profile image
Hila Berger

Thanks!
what about legacy code? what is your definition for that?

Thread Thread
 
nestedsoftware profile image
Nested Software • Edited

Usually the term "legacy code" refers to an older codebase that’s still in use but the technology is considered somewhat outdated and therefore poses greater maintenance difficulties. It does not seem to be related to BDD though. If your question was related to BDD somehow, please clarify.

Thread Thread
 
hilaberger92 profile image
Hila Berger

It's not related to BDD. Legacy code has many definitions, so I wanted to know what's yours :)
My favorite definition is:"Code you are afraid to change".