Unit testing
Unit testing, also known as component or module testing, is a form of software testing by which isolated source code is tested to validate expected behavior. Unit testing describes tests that are run at the unit-level to contrast testing at the integration or system level.
Unit testing, also known as component or module testing, is a form of software testing by which isolated source code is tested to validate expected behavior. Unit testing describes tests that are run at the unit-level to contrast testing at the integration or system level. ==Code visibility== Test code needs access to the code it is testing, but testing should not compromise normal design goals such as information hiding, encapsulation and the separation of concerns. To enable access to code not exposed in the external API, unit tests can be located in the same project or module as the code being tested. In object oriented design this still may not provide access to private data and methods. Therefore, extra work may be necessary for unit tests. In Java and other languages, a developer can use reflection to access private fields and methods. Alternatively, an inner class can be used to hold the unit tests so they have visibility of the enclosing class's members and attributes. In the .NET Framework and some other programming languages, partial classes may be used to expose private methods and data for the tests to access. It is important that code solely for accommodating tests does not remain in the production code. In C and other languages, compiler directives such as #if DEBUG ... #endif can be placed around such additional classes and indeed all other test-related code to prevent them being compiled into the released code. This means the released code is not exactly the same as what was unit tested. The regular running of fewer but more comprehensive, end-to-end, integration tests on the final release build can ensure (among other things) that no production code exists that subtly relies on aspects of the test harness. There is some debate among developers, as to whether it is wise to test private methods and data anyway. Some argue that private members are a mere implementation detail that may change, and should be allowed to do so without breaking numbers of tests. Thus it should be sufficient to test any class through its public interface or through its subclass interface, which some languages call the "protected" interface. Others say that crucial aspects of functionality may be implemented in private methods and testing them directly offers advantage of smaller and more direct unit tests. class Adder { public int add(int a, int b) { return a + b; } } The test suite uses assert statements to verify the expected result of various input values to the method. import static org.junit.Assert.assertEquals; import org.junit.Test; public class AdderUnitTest { @Test public void sumReturnsZeroForZeroInput() { Adder adder = new Adder(); assertEquals(0, adder.add(0, 0)); } @Test public void sumReturnsSumOfTwoPositiveNumbers() { Adder adder = new Adder(); assertEquals(3, adder.add(1, 2)); } @Test public void sumReturnsSumOfTwoNegativeNumbers() { Adder adder = new Adder(); assertEquals(-3, adder.add(-1, -2)); } @Test public void sumReturnsSumOfLargeNumbers() { Adder adder = new Adder(); assertEquals(2222, adder.add(1234, 988)); } }
References
No reference rows published for this article.
Cite this article
MLA
"Unit testing." encyclopegia.org, 2026, https://encyclopegia.org/wiki/unit-testing. Accessed 24 November 2026.
Chicago
"Unit testing." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/unit-testing.
APA
encyclopegia.org. (2026). Unit testing. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/unit-testing
Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON