Test Driven Development is the way to go. But how to do that?

A number of developers develop their applications using Test Driven Development (TDD), but I would like to know at what stage should I include TDD in my projects? Should I first develop my classes, do some unit tests and determine which methods are needed or develop methods, and then do some unit tests afterwards?

What is the best way to do this?

+4
source share
9 answers

The purpose of this test is that tests drive design as well as implementation.

In some cases, this is not suitable - it may be obvious what the design should look like, especially if it implements an existing interface (or some similar limited choice), but when you have a large “design space”, TDD encourages you to write tests that demonstrate how you want to use this class, and not start with you, as you think you want to implement it.

Usually, if something is easy to verify, it will be easy to use.

+18
source

TDD is a coding and design method in small. This is not a very attractive technique. If you are starting to write an application, you want to make some storyboards, wireframes, or even simple sketches. You should be aware of a larger design, that is, classes and relationships in your system. Before moving on to the point where you begin to implement the interaction design (for example, methods and arguments), you start doing TDD.

The frameworks you made will give you an idea of ​​how the system should look. Large-scale design will give you an idea of ​​which classes to create. However, none of these models should be considered correct or permanent. When you write tests, you will find the best design ideas that will change your design to a high level and even your frame design.

+16
source

Mantra for test design:

  • create a test.
  • compile the test (and make sure that it does not work)
  • write interface
  • compile the test (it should now compile)
  • run the test (it should fail)
  • write code
  • compile / run the test (it should do both)

Repeat for each item.

+6
source

I am very skeptical about writing unit tests before implementation. Maybe it would be effective if you had a good idea of ​​the exact design of the classes and methods, but usually you do not do this at the beginning of a new project.

My preferred approach is to admit that starting a new project can be a bit of organized chaos. One part of the design process is writing code to test certain ideas or find the best way. For a long time, the code gets stuck and is discarded, and any unit tests written for it would be a waste of time.

Do not misunderstand me. I'm all for well-designed software, and at some point it is important that organized chaos turn into a well-understood design. At this point, the class structure and UML diagrams begin to solidify. This TDD point is becoming important to me. Each class must be tested in isolation. If the design is good with the right degree of isolation, which is usually easy to achieve by spraying piece objects around the site. If the design of poor testing becomes a real nightmare and, therefore, it is ignored.

At the end of the day, this is the quality productive code that you need. Unit tests are one of the best tools to achieve this, but they should not take on life and become more important than the ones they experience that you sometimes feel from the TDD mantra.

+2
source

Start small

a) The next time you add a method to the Utility class, first write tests for this method. The methods that perform file operations are a good place to start, since they usually have a lot of errors, but they do not require a complex set of data to test.

b) Then, as soon as you receive a power of attorney with written unit tests, proceed to classes that do not concern the user interface and do not speak directly or indirectly with the database.

c) Then you need to decide whether you will develop an application to simplify unit testing. For instance,

  • It's hard to check the user interface code, so look at MVP, etc.,
  • Head to verify the database access code, so separate your logic into a method that is not needed to access the database to run.

a) and b) has a large payback, without changing how you develop a common system. You should consider if c) is worth the cost .... (I think so, but many people do not, and often you cannot control the overall design of the system.)

+2
source

Ideally, you will start using TDD when you start coding. Think about your design, you can draw diagrams on paper; you don’t need to use the CASE tool, just imagine the main classes and their interactions, choose one, write the first simple test, write the code to pass, write another test, do it, refactoring ...

If your process requires documentation and analysis of the design before the coding stage, you cannot follow the TDD and let your design appear. However, you can do your project first and then run an implementation test. You will get unit tests as if you were using TDD, but applying your process anyway.

Keep your design as high as possible so that you can let the details of the design down.

+1
source

Take a look

http://en.wikipedia.org/wiki/Test-driven_development

They announced high-level challenges.

0
source

TDD ensures that developers are equally important for testing cases compared to their business logic. I followed him in my project, and I could see the benefits. Since I started writing my test cases first, I was convinced that my test cases handled all possible coverage of business logic, thereby catching / reducing the number of errors. This is sometimes impractical, since it takes time to write these test cases. The bottom line is that you need unit test your code is really good. So either you start with the implementation, and then make sure that there are enough test cases for all the scenarios (most people haven’t come from this practice.) Or write test methods to make a dry run, and then write your implementation. Your implementation is considered complete only when all test tests have passed successfully.

0
source

You should listen to podcast 41, where they talk about TTD. It's hard to say that everyone uses TDD, and it will take a lot of time and resources to have a high percentage of code coverage. Unit testing can effectively double development time if you write tests for all of your code.

One point of the podcast is that your time and resources could be better utilized by performing other tasks, such as usability and new features.

0
source

All Articles