2.2 Unit Testing: Driver, Stub

22518 Software Testing MSBTE CO IT 2.2 Unit Testing: Driver, Stub

 

Unit Testing

            Unit testing is the first level of software testing where individual components of a software are tested. The purpose is to validate that each unit of the software performs as 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 function or procedure.

            Unit testing is typically performed by the developers themselves. They isolate the code from the rest of the software system to ensure that the test conditions are controlled and can be reproduced consistently. This helps in identifying and fixing bugs in the early stages of the development cycle, which can save both time and money.

Here are some important points about unit testing:

  1. Isolation of code: Each unit test should be independent from others and should not rely on any other unit tests.
  2. Framework usage: Unit testing is often automated and is run every time a change is made to the software to ensure that all existing functionality remains intact. This is often done using frameworks such as JUnit for Java, NUnit for .NET, Pytest for Python, and so on.
  3. Mocking: Since unit tests are isolated, they often make use of "mock" objects to simulate complex real objects such as database objects, network sockets, etc.
  4. Coverage: While writing unit tests, developers aim for a high "code coverage", meaning that the tests exercise as much of the codebase as possible.
  5. Early bug detection: One of the major advantages of unit testing is that it can catch issues and errors early in the development cycle, making them typically less costly to fix compared to those found at later stages.

Remember, while unit tests are extremely useful for checking the correctness of your code, they cannot catch every type of error. Other types of testing, like integration testing, system testing, and acceptance testing are also needed to create a robust software application.

 

Unit Testing: Driver, Stub

In the context of unit testing, the terms "driver" and "stub" are used to describe two types of code created to facilitate testing. Here's a breakdown of these terms:

  1. Driver: A driver is a piece of code that is used to invoke a module or component that is being tested and provide it with the necessary input. In unit testing, when a module is under test, it often calls procedures in other modules. If those other modules have not yet been developed or they're not yet ready for testing, a driver can be used to simulate those procedures. Drivers are also known as "test harnesses."
  2. Stub: A stub is a piece of code used to stand in for some other programming functionality in software testing. It's used to simulate the behavior of complex components in a simple way. If the unit under test has any dependencies, a stub is used to mimic the behavior of those dependencies so that the unit can be tested independently.

For example, if a function that you're testing sends a request to a database, you might create a stub that pretends to be the database and just returns a constant result. Stubs are used in bottom-up integration testing, where testing starts from the modules at the lower levels of program hierarchy.

In simple terms, a driver calls a stub, and the stub is called by the module being tested. This way, the module can be tested in isolation, without any dependencies on other pieces of code. This is essential for unit testing where the focus is on testing individual components in isolation from the rest of the system.


Post a Comment

Previous Post Next Post