Search code examples
c++testingtimemicrosoft-cpp-unit-test

How to set Timeout in CppUnitTestFramework (C++) in Visual Studio?


How to Add Timeout for a test method in C++ in Microsoft unit testing using CppUnitTestFramework ? Most of the Solutions I found online are for CSharp projects where I can add lines like [TEST_METHOD,TIME_OUT(80)] or such ,but those are not working while testing C++ (VC++) code

I have tried the below code

#include "stdafx.h"
#include "CppUnitTest.h"
#include "../src/factorial_dp.cpp"
#include "stdio.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace spec
{
    TEST_CLASS(factorial_dpSpec)
    {
    public:

        //Add Timout for these methods
        TEST_METHOD(Smallnumber)
        {
            int result = fact(5);
            Assert::AreEqual(120, result, L"5 fact should be 120", LINE_INFO());
        }

    };
}

Solution

  • Use managed test classes. and you can keep Timeouts in that .

        [TestMethod(), Timeout(3000)]
        void functionName()
        {
        //
        }