When not / write tests before implementation?

I'm not sure how “tests first” works, and I would like to hear arguments about when and why this approach could be used.

I heard that before writing a single line of implementation, it is often recommended to write tests and mock them. However, I cannot help but think that this is not appropriate for any situation. For example, let's say I'm making a prototype, and I'm not sure how everything will work. So I’m just starting to find examples of each step that I think I need and throw them into my code. In the end, I have a proof of my theory, and it doesn't take so long. This is essentially "my test." This is not a unit test, but a test (most likely a console application).

This is pretty much how I work. I think about what I want to do and try to do it. If that works, I will eventually come back and write unit tests so that I can trap the regression. Is this different from what you “should do”?

+3
source share
7 answers

Strikethrough Rule: Do the most dangerous items first.

The execution of test scenarios, first of all, implicitly, claims that the most risky part of coding is connected with incorrect communication and misunderstanding of interfaces and behavior of created objects.

For many projects, this may be true, and TDD is very suitable in these cases.

However, in many projects this is not the case, and using TDD in these cases is a poor choice.

If your highest risk is usability, stop working with unit tests and follow some UI prototypes.

If your high risk is performance, first prototype performance and don't worry about interfaces.

The list goes on.

Performing risky items at first has many advantages:

  • Projects that are inevitably doomed die early before many resources are wasted.

  • Projects that are in trouble but are savings are gaining the focus of project management at an early stage when it can be beneficial.

  • The business side of your organization will evaluate the project higher if it has a low risk of failure; there is less chance that it will be canceled early unnecessarily.

+7
source

"I heard that it is often recommended to write tests and mock things before writing a single line of implementation ... I end up going back and writing unit tests ... Is this any different than what you should do?"

Since you started by answering your question, do you really not ask this question?

There are many reasons why people answer their questions. Sometimes this is a way to argue.
This allows people to say: "I do not argue, I'm just asking why this is so wrong."


The goal first is the test. Here's how it works.

Say I'm making a prototype, and I'm not sure how things will work.

However, I know one thing. What should he do.

  • Write down a concrete example of what it should do. Concrete, specific inputs and outputs.

  • This is a test case. I did it first. Can I formalize this as a unit test? Probably no. However, I started with an acceptance test.

Now I can break the problem into pieces.

  • So, I'm just starting to find examples of each step that I think is necessary.

  • For each example, something that I think I need, I write down what happens and what comes out of this step.

  • These are test cases. I made them first. In many cases, I can formalize them as unit tests.

  • As soon as I have a test, I return to the example of each step and throw it into my code.

I tested, then coded. I have not done ALL testing before ANY encoding. At first I tested, but not in crazy mode without test code. I did this in an incremental test-small-code-a-bit. But first, everything was checked.

+3
source

It is simple, the answer during prototyping. At this stage, you don’t understand the system of your building well enough to test correctly, and in any case, good practice says that the prototype code should be discarded code, so testing at this stage will not give you real advantages. But the insights that shone from prototyping will help you so that you can do effective testing as soon as you enter production.

So yes, your approach is correct if you test the prototypes

+2
source

I think your approach of not testing the spike / prototype is just fine. But two ideas:

  • as soon as you finish your prototype and know what you are doing, either throw it away, or repeat it first, or write tests for the code that you have already written.

  • when you had more practice tests, you could quickly find your prototype in the test than create a console application. I do not mean creating tests and a separate class with an idea; I mean exploring w / code directly in the testing method. I have done this several times, and I have been very pleased with it. When I learn a new api or even a new language, the test gives me the fastest feedback loop for experiments. Then, as soon as the code works, I can extract it into a separate method / class to be part of the real system.

+1
source

I believe that the first tests are not written as well when I am still building a history of my code. It's hard to write tests when I don't know what the interfaces look like. I could write stub code to display classes and interfaces without thinking about the tests. But I try to pass the tests as quickly as possible. I find this helps if I take notes on things that I would test when I create a design, and then when my design hardens, I return to my notes and I do these tests first. This usually means that the implementation and unit test code grows together, not one before the other.

0
source

There is no single “right way”. However, I really think Test Driven Development (TDD) will help in your case. I believe that writing tests first helps shape the API and makes the code cleaner. When you first write tests, you first think about how the code will be called (the interface or “what should it do”) before thinking about the implementation (“how do I do this”).

For example, imagine creating a CRM module. You might think that the first thing to do is get the client to spend the most money. So you should write a test:

Assert.AreEqual (Customer1, crm.GetMostValuableCustomer (), "the most valuable customer is not as expected");

Then you can do the following:

Assert.AreEqual (new customer [] {Customer1, Customer2, Customer3}, crm.GetCustomerByValue (), "GetCustomersByValue () is not as expected");

So, the point is that you think about the code from a different perspective (as a consumer, not as a producer). I believe that helps me write much cleaner code, and then I don’t need to go back and create regression tests later. I wish I had a better example, but I hope you will see how working with this method can help you in the prototyping phase.

0
source

Even when you are writing an outlier prototype, it’s still useful to think about what the prototype will actually do and start with tests that confirm that it does. Providing prototype validation will also determine how the solution approaches.

Then, when the code is thrown, you still have tests.

I also have problems with prototype testing, but when I succeeded, I was always glad I did.

0
source

All Articles