주요 콘텐츠

Differences Between Stubs and Mocks When Testing C/C++ Functions

When authoring a unit test for a C/C++ function in the Polyspace Platform user interface, you can replace a callee of the function with an alternative definition using one of these ways:

Both ways to replace a callee involve similar steps and use the graphical interface for authoring. However, there are some important differences between stubs and mocks:

  • Stubs are necessary and mocks are optional.

  • In a project, you can define multiple mocks, but only one stub for a function.

  • Stubs are project-specific while mocks are test-specific.

Stubs Necessary, Mocks Optional

Stubbing undefined functions is essential, but using mocks to override function definitions is optional.

Stubs stand in for undefined functions in the call hierarchy of a project or test. If a function under test calls an undefined function, the test fails to build until the function definition is available. Stubs provide the missing definition and allow test to build without errors. Therefore, stubbing an undefined function is essential for successful building and running of tests.

Mocks override functions that are already defined. Mocks provide a convenient way to replace function callees that are irrelevant for a test but take up test authoring and test execution time. For instance, if a function callee reads a data file, to test the function, you have to prepare a data file upfront. The file read operations also take up unnecessary time during testing. Replacing this function callee with a mock can shorten test preparation and test execution time. However, the replacement is not essential. It is possible to retain the default implementation of the function callee and still author and execute a test.

One Stub, Multiple Mocks

You cannot define multiple stubs for a function. Once an undefined function is stubbed, the stub acts as a substitute for the function. The stub definition can be edited later, but another stub cannot be associated with the function.

You can define multiple mocks for a function. The mocks for a function func are named func_mock, func_mock_1, func_mock_2, and so on. When authoring a test for a function, you can replace a callee by picking from the list of available mocks.

You can define a single mock or stub that has different implementations in different tests. For more information, see Change Mock or Stub Implementation Based on Test.

Stubs Project-Specific, Mocks Test-Specific

A function stub is unique across a project, but a function mock is specific to a test within a project.

A stub for an undefined function acts as a permanent replacement for the function. In tests where the function appears as a callee, the stub is used instead. In this sense, a function stub is unique across a project.

A mock for a function might or might not be used in tests. Even if a function has mocks available, in each test, you have the option to pick one mock from a list or not pick a mock at all. In this sense, each test in the project has a specific set of mocks.

See Also

Topics