I read articles about TDD - everything is clear with the approach itself. I tried it a couple of times, but somehow it doesn’t work and now I’ve come back to this question again.

The question is this - for example, I decided to use TDD, but how can I come up with tests if I don’t know what errors might be? As I understand the bug is an unexpected behavior of the program and in terms of this approach (TDD) I do not understand how it can be foreseen. In my case, I often forget to check / write something and often get bugs, even though I'm a relatively experienced developer. It happens like this - I will write something, test it locally and everything is OK, upload the server to the sandbox and the tester will somehow not so click (for example) and get an error, but I haven’t tested it before - I haven’t found it. In general, there is no understanding how to properly think through all the tests in advance and check that there are no errors in the future. I hope I wrote clearly :)

  • if you have no errors, it means either qa is bad or the compiler is broken. Mistakes are normal. Therefore, write more projects, and, over time, understanding will come. - KoVadim
  • Since the question is now being demolished, as not suitable for the subject of the resource, I will advise you a book ozon.ru/context/detail/id/136632098 - Artyom Okonechnikov
  • And I advise you not to close on a specific technique. You can not use TDD, it is not necessary. The main thing is to write tests at least in parallel with the code, so it will be easier for you than to change the code already written - Artyom Okonechnikov
  • @KoVadim is not about the fact that there are no errors. It's about how to anticipate max. the number of mistakes in advance is to come up with tests for them and after that write the code already - Pavel Lankmiler
  • Even clairvoyants cannot foresee (Otherwise everyone would be millionaires). Therefore, only experience. - KoVadim

1 answer 1

Not all applications are equally suitable for TDD.

In general, TDD says that the test should be written before writing the code. That is, you must first describe what the application should do (TK), then write the skeleton of the application with empty functions, then tests for these functions (which at first everyone will produce errors), and then write the code for the functions.

Do not write tests for testing ready-made functionality. Tests must be born before the code, or because of detected bugs, or before refactoring (to check for persistence behavior).

Example: you need to write an application to calculate the cost of hull insurance. You write TZ, you write architecture (something in the spirit of MVP), you write stub functions in the model that consider the cost. Next, write the tests that feed the data into them and check for correct results. The more marginal cases describe, the better. And then write the code (and supplement the tests if any tricky branches appear in the code).

Testing the interface and control components is not so simple and usually is not done, because not worth the candle, easier to test hands. The model, in any case, should still validate the input data from the control (thus, the interface must not be able to cause erroneous behavior of the model due to its bugs)

  • In principle, I understand this - it's just interesting how to think through the tests themselves. For example, how to think up a June programmer’s tests if he doesn’t know what can happen in principle at all. For example, in the online store there is a payment by card and he has no idea what expelling may occur (sometimes it happens that I didn’t finish reading the docks for example) Thanks for the answer - he stressed some things for himself :) - Pavel Lankmiler
  • one
    TDD, and indeed tests (and indeed anything else), are not a silver bullet. There will always be classes of problems that no one thought of or provided for. TDD is just one of the methodologies to help look at problem solving from the other side and, thus, to notice and provide for the solution of some of these problems. TDD helps to better structure the code, encourages black boxes and OOP. - Kromster
  • The tests themselves are very easy to invent. In the test code, you express your assumptions about what the code does. const str = "{'key':'value'}"; const obj = JSON.parse(str); assertEquals(obj.key, 'value'); In this case, you expect the JSON.parse method JSON.parse turn the string into an object. After writing the test, write the implementation of the method and run the test. There are lots of great books on this topic. The authors, for example, Kent Beck or Robert Martin. - SergeyEgorov pm