How to test my C++ function

Jiaxin Zhang 60 Reputation points
2025-10-09T02:13:43.14+00:00

How to test a C++ function?

My function returns wrong results. I’m debugging.

The only way I can think of is creating a new console application, then copy the function into it and input test data one by one using keyboard.

Do you have better methods?

Thanks!

Developer technologies | C++
Developer technologies | C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
{count} votes

Answer accepted by question author
  1. Varsha Dundigalla(INFOSYS LIMITED) 2,700 Reputation points Microsoft External Staff
    2025-10-09T07:48:16.5866667+00:00

    Thank you reaching out.

    Recommended Microsoft Tools & Methods to Test C++ Functions

    1. Microsoft Native Unit Test Framework (Visual Studio)

    This is the built-in unit testing framework for C++ in Visual Studio.

    Steps:

    1. Open your C++ project in Visual Studio.
    2. Add a new Native Unit Test Project via File > Add > New Project.
    3. Write test cases using TEST_CLASS and TEST_METHOD.
    4. Use Assert::AreEqual() or other assertions to validate your function.
    5. Run tests using Test Explorer (Test > Test Explorer).

    Benefits:

    • Integrated with Visual Studio.
    • Supports code coverage and test traits.
    • Works with Live Unit Testing (Enterprise edition).

    2. TAEF (Test Authoring and Execution Framework)

    Used internally at Microsoft, TAEF supports writing automated tests in C++.

    Steps:

    1. Install TAEF via internal Microsoft channels or Visual Studio extensions.
    2. Write test cases using TEST_CLASS and TEST_METHOD.
    3. Use VERIFY_ARE_EQUAL() for assertions.
    4. Run tests using TShell or Visual Studio integration.

    Benefits:

    • Supports logging and diagnostics.
    • Used across Windows and platform teams.

    3. Visual Studio Debugger

    Ideal for step-by-step inspection of your function.

    Steps:

    1. Set breakpoints in your function.
    2. Use F5 to start debugging.
    3. Inspect variables using Watch, Locals, and Immediate Window.
    4. Step through code with F10 (Step Over) and F11 (Step Into).

    Benefits:

    • No need to copy code into a separate console app.
    • Helps identify logic errors and incorrect values.

    4. Live Unit Testing (Visual Studio Enterprise)

    Automatically runs tests as you write code.

    Steps:

    1. Enable Live Unit Testing via Test > Live Unit Testing > Start.
    2. Write unit tests using the Native Unit Test Framework.
    3. See inline results and coverage directly in the editor.

    Benefits:

    • Continuous feedback.
    • Highlights failing tests and uncovered code.

    5. Microsoft Test Manager (MTM)

    For managing manual and automated test cases.

    Steps:

    1. Use MTM to create test plans and test suites.
    2. Link test cases to your C++ functions.
    3. Run tests and log results.
    4. Integrate with Azure DevOps for traceability.

    Benefits:

    • Ideal for larger projects and team collaboration.
    • Supports manual testing workflows.

    References for Future Use

    Please let us know if you require any further assistance, we’re happy to help.

    If you found this information useful, kindly mark this as "Accepted Answer".


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.