Unit Tests

In this part I will talk a little about unit testing framework, I choose for the example the language of Java.
UNIT TESTING, its a level of software testing where individual units or components of a software are tested. The purpose is to validate that each unit of the software performs as is was designed. A unit is the smallest testable part of any software. It usually has one or a few inputs and usually a single output. In procedural programming, a unit may be an individual program, function, procedure, etc. In object-oriented programming, the smallest unit is a method, which may belong to a base or super class, abstract class or derived or child class.

descarga
It has many benefits:
• Increases confidence in changing/ maintaining code.
• Codes are more reusable
• Development is faster
• Lesser cost in fixing
• Debugging is easy
• Codes are more reliable
For my example I will use JUnit:
It is a test framework which uses annotations to identify methods that specify a test.
For JUnit test, is a method contained in a class which is only used for testing. This is called a Test class. To define that a certain method is a test method, annotate it with the @Test annotation. This method runs the code under test. You use an assert method, provided by JUnit or another to check an expected result versus the actual result. These method calls are typically called asserts or assert statements.
This is a great tool that should help you save a lot of time and energy going back over code that isn’t working right because it wasn’t regularly tested.
The code that I used can be found here:
https://github.com/alexdelmed/Unit-Tests/tree/master

cleaner-better-code-670x335

In here we test that the method of JunitTest, that takes a string and counts the number of time designated letter occurs in the string.

I used Netbeans for this.

The code JunitTesting.java is the program that will run.

aaaa

Then Click on Tools and Create/Update Tests(You have to be on the Operator class)
The code JunitTestingTest.java is to verify that it is correct.

vbn

Then Run the Test program with «Run» -> «Test Project» and i will pop the results.

 

References:
https://www.tutorialspoint.com/junit/junit_test_framework.htm

thanks a lot to this blogg:

How to use JUnit with NetBeans

Deja un comentario