Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use debug vs unit tests?

I'am a little bit confused what is better to use debug or write unit test? and is this general or there are cases where debug better than unit test?or should I use both of them?

Thanks

like image 459
Feras Odeh Avatar asked Oct 02 '10 14:10

Feras Odeh


People also ask

When should program debugging be performed?

In software development, the debugging process begins when a developer locates a code error in a computer program and is able to reproduce it. Debugging is part of the software testing process and is an integral part of the entire software development lifecycle.

What comes first testing or debugging?

Testing is a stage of software development life cycle (SDLC). Debugging is not an aspect of software development life cycle, it occurs as a consequence of testing.

Is debugging a unit test?

Debugging is a process of line by line execution of the code/ script with the intent of finding errors/ fixing the defects. Unit testing is often automated but it can also be done manually. Debugging is a process of line by line execution of the code/ script with the intent of finding errors/ fixing the defects.

When should you run unit tests?

Run all your unit tests as often as possible, ideally every time the code is changed. Make sure all your unit tests always run at 100%. Frequent testing gives you confidence that your changes didn't break anything and generally lowers the stress of programming in the dark.


2 Answers

Unit test is used to ensure that code works as expected. Debug is used when you need to find why the code doesn't work as expected.

like image 143
Ladislav Mrnka Avatar answered Sep 19 '22 07:09

Ladislav Mrnka


Debugging will help you diagnose non-working code.

Unit tests provide the following:

  1. a repeatable means of determining that your code works, both for common scenarios and for edge cases. They will allow you to refactor your code with confidence that it still works.
  2. a demonstrable specification of the code. In the absence of a written specification your unit tests are your code's specification. This is particularly true in the Agile world.
  3. an aid to building well-structured code. Because you want to run unit tests standalone, you have to write your classes under test to accept different (sometimes mocked) datasources, sinks etc. By encouraging these abstractions and separation of concerns, your code will become well-structured (you can, of course, write well-structured code without unit tests)

Your unit tests should be run repeatedly (most often as part of your build process). If you do break them (most often due to a programming error), then it's time to break out the debugger to identify the issues and fix up the code (or perhaps amend the test) accordingly.

like image 35
Brian Agnew Avatar answered Sep 21 '22 07:09

Brian Agnew