4

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());
        }

    };
}
abhirulz
  • 87
  • 1
  • 2
  • 11
  • I googled your title (well the three most relevant words) and found (http://codereview.stackexchange.com/questions/84697/timeout-watchdog-using-a-standby-thread). Should maybe do the trick. – Cheers and hth. - Alf Jan 07 '16 at 05:30
  • I have read it ,this line ìs what is being tested for time . std::this_thread::sleep_for(std::chrono::milliseconds{ 1 }); Is there any way to replace that line with any given function ,or infinite loop ,Its not working in that case – abhirulz Jan 07 '16 at 05:58

1 Answers1

1

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

    [TestMethod(), Timeout(3000)]
    void functionName()
    {
    //
    }
Tirupati Rao
  • 615
  • 6
  • 24