-2

I have read many articles explaining the overview of black- and white-box testing, including this Stack Overflow question: https://stackoverflow.com/questions/7258524/unit-testing-black-box-testing-and-white-box-testing

However I still have some confusion around black-box and white-box testing at a basic level.

In terms of a specific example using Python's doctest module, I want to know if I am dealing with black- or white-box testing. My code is below.

My guess from what I have read is that this kind of testing is classified as white-box, if I am writing the code as well as the tests. However, what if I were to ask someone else to produce the comment section which produces the tests and then to run the code - would this then be black-box testing?

Perhaps my question is really whether black-box vs white-box categorization is inherent in the code itself, or the context in which the code is run?

The simplicity I am seeking is what to tell as student who is learning about different types of testing whether using doctest is a white-box or black-box technique or neither or "it depends."

Additionally I would also like to know whether the answer would be the same for the kind of tests present on sites like Hacker Rank and Codewars which look something like assert_equals(my_function(args), expected).

import doctest

def fib(n):
    """ 
    Calculates the n-th Fibonacci number iteratively  

    >>> fib(0)
    0
    >>> fib(1)
    1
    >>> fib(10) 
    55
    >>> fib(15)
    610
    >>> 

    """
    a, b = 0, 1
    for i in range(n):
        a, b = b, a + b
    return a

if __name__ == "__main__": 
    doctest.testmod()
3

The simplicity I am seeking is what to tell as student who is learning about different types of testing whether using doctest is a white-box or black-box technique or neither or "it depends."

The answer is "it depends". Black box vs white box isn't distinguished by the tool you are using. Do you know how the functions are implemented and thus test according to the implementation (white box testing -- you can "see inside the box"), or is the implementation irrelevant and you are testing according to the behavior of the function (black box testing - you can't "see inside the box").

In your specific example, the test is black box since it doesn't consider how the function is implemented, only that it returns the expected values for the given inputs. Though, in such a simple case the distinction is meaningless.

Think of testing a car engine. Black box testing is asking whether the engine runs faster when the accelerator is pressed. White box testing is asking whether the throttle opens up and delivers more fuel to the engine, and that when more fuel is delivered then the pistons move faster, and that when the pistons move faster the driveshaft moves faster.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.