Help me understand how to write a test (TDD)

I’ve done this: Why Test? Cake O’Clock

I understand the code and everything and I succeded to write the last test on my own.

However I dont understand how this works in the real world.

it('displays the selected hour', () => {
  const hour = '9:00';

  browser.selectByVisibleText('#select-pickUp', hour);'#submit-order');

  assert.include(browser.getText('#pickUp'), hour);

This is a very detailed test. Are you supposed to write this kind of test for everything on the page?
For every selector, every choice the user makes and every label?

In this situation we manually found the error and then wrote the test. So if you write the test after you find a bug, why dont you just correct the bug without writing that test-part?

There is not an absolute answer possible here, I would say you could maybe could boil it down to two test: one with valid data and assert the response (which you do in this test) and write a test with invalid data (for example 36:00 hours, given there are only 24 hours in a day) and assert that an error is displayed

because the test reduces the change of breaking something (again) in the future. You might need to make changes to existing code 3 months down the line, then the test can be a massive help.

As projects grow its, nice to have some fail-safes, of course you need to aware that tests aren’t bullet-proof/air-tight.

The project I currently work on has over 400 test, if I had to test everything manual every time, it would cost me an awful lot of time

1 Like

wow 400! thats alot :sweat_smile:

So I guess in a large project like that it acctually can save time in the long run to write that big test one time. And if another bug shows up you can just add that to the test so it does not happend again.

I think I get it now, thanks!

I mostly do back-end, so it might be somewhat different then the front-end. My test-suite is divided into Feature and Unit test. Where a unit-test is a small test (for example a complex piece of business logic) while the feature test I make a fake request and do assertions on the response and assert the data exists in the database

Test can also help you to understand the behavior of your code. It can happen you need to extend a feature of your application after three months for example.

Test also force you to think about the structure of your code, given good structured code is easy to test small pieces and the whole package as well

Here in also lies the problem: I truly began to see the value of tests, abstractions, classes and more once your projects starts growing. When organizing your code, ensuring your code is readable and maintainable in the long run becomes more important

You seem to understand it well now :slight_smile: With more practice you will gain an even better understanding. Some things can only be learned through experience.

1 Like