software, testing, service-762486.jpg

How to Test Code for Bugs? And What Is Unit Testing?

Outline:

  1. Bugs
  2. Testing Levels
  3. Characteristics
  4. Testing Options
  5. What makes a good test
  6. Environments
  7. Unit Test

Bugs:

Bugs are inevitable in any complex software system. A bug can be visible or can hide in your code until much later. Industry estimates: 10-50 bugs per 1000 lines of code. That is why we should reduce code, test it and then reuse it for multiple more times with confidence that it is bugs-free.

Software reliability:

It is the probability software system will fail under specified conditions. It could be measured by uptime, MTTF (Mean Time till Failure), crash data, etc. Therefore, we need to test our code to make sure it works as expected under all condition and to make sure it does not do something it should not.

Find out more about what are software bugs and how to measure software reliability…

Testing:

Why:

  • Prevents known errors.
  • Prevents the introduction of bugs.
  • It is difficult to test for everything so some bugs will always remain and we always need to try our best to find them.

Two types:

  • Happy Test – Testing positive scenarios where you have clean cases that go through the system.
  • Fail Scenarios – Test of bad scenarios that where things go wrong.
Test and Code

Testing Levels:

Testing levels

Code review: Let other programmers read your code to find bugs. This is good specially because it makes you write a more readable code that others will need to read and understand. Which means, it would be also easier for somebody else to maintain it later and add new features.

Unit testing: A low-level testing when we test only one specific scenario. More about it later in another section.

Integration testing: It is when you are testing with another application. For example, if your software calls a database, does it work with that or not?

API testing: More specific version of Integration testing. It is when you are testing for defined specific ways of calling another application to make sure what the information will be coming in and what the respective responses are.

GUI testing: Testing your application visuals. Is it looking as expected?

Characteristics:

  • Functionality – Testing the function, business use of the application.
  • Usability – Testing to speed to enter the easy of use of the application.
  • Security/ Penetration Testing – Testing if it is secure and can survive attaches.
  • Performance/Efficiency – Testing how fast and good the memory is of the application.
  • Portability – Can the application be deployed on different browsers, operating systems, web or mobile applications.
  • Reliability – How resilient is the application to breaks and how quickly can it be restored.

Testing options:

  • Ad-hoc – Test run on demand and when required
  • Test Suite – A set of tests run together
  • Code coverage – The number of lines of code that tests will cause to be executed

Whet are you relying on:

  • Time outs – Calling an application that does not reply in time
  • Broken connections – A database losing it connection, network issues
  • Downtime – An application being down or not available
  • Disaster Recovery – If your main production site goes down what happens, the back up to run when production has problems

What makes a good test:

  • Testing for Edge cases.
  • Different Volumes – 1,2,3,50,5000.
  • Multiple calls to the same class/method.
  • Empty, negative values.
  • Positive values.
  • Failures.
  • Combinations of all above.

Environments:

  • Development or local environment
  • System Integration Testing (SIT)
  • User Acceptance Testing (UAT)
  • Staging
  • Performance Testing
  • Disaster Recovery
  • Live or Production

Unit testing:

Testing one method at a time under a specific condition and with specific values.

In Java: use

Import org.junit.jupiter.api.Test;

Then Import class to test.

Unit testing image

Options:

  • AssertTrue
  • AssertFalse
  • assertEquals – String
  • assertEquals – Double
  • assertEquals – Integer
  • assertEquals – List, array
  • assertSame
  • assertNotSame
  • assertNull
  • assertNotNull
  • Exception

States:

  • Public – can test and be seen by other classes
  • Private – not visible for test or other classes
  • Protected – visible for test, but not from other classes

Conclusion:

  1. Testing characteristics: Functionality, Usability, Security/ Penetration Testing , Performance/Efficiency, Portability, Reliability.
  2. What makes a good test: control your environment and know what values to test for.
  3. What is unit testing: Testing one method at a time under a specific condition and with specific values.

Leave a Comment

Your email address will not be published. Required fields are marked *