FB_TcUnitRunner  Function Block

This function block is responsible for holding track of the tests and executing them.


Variables

Name Type Default Description
AllTestSuitesFinished BOOL FALSE
AllTestSuitesFinishedTrigger R_TRIG
TestResults FB_TestResults
AdsTestResultLogger FB_AdsTestResultLogger(TestResults)
TestResultLogger I_TestResultLogger AdsTestResultLogger
AbortRunningTestSuites BOOL
xUnitXmlPublisher FB_xUnitXmlPublisher(TestResults)
XmlTestResultPublisher I_TestResultLogger xUnitXmlPublisher

Methods

AbortRunningTestSuiteTests INTERNAL

This function sets a flag which makes the runner stop running the tests in the test suites

Implementation
AbortRunningTestSuites := TRUE;
RunTestSuiteTests INTERNAL

This runs all the test suites in parallel

Implementation
IF GVL_TcUnit.StartedAt = 0 THEN
    GVL_TcUnit.StartedAt := F_GetCpuCounterAs64bit(GVL_TcUnit.GetCpuCounter);
END_IF

// Run TcUnit test suites
IF NOT AllTestSuitesFinished THEN
    IF GVL_TcUnit.NumberOfInitializedTestSuites = 0 THEN
        AllTestSuitesFinished := TRUE;
    ELSIF GVL_TcUnit.NumberOfInitializedTestSuites > 0 THEN
        FOR Counter := 1 TO GVL_TcUnit.NumberOfInitializedTestSuites BY 1 DO
            IF GVL_TcUnit.TestSuiteAddresses[Counter]^.AreAllTestsFinished() THEN
                NumberOfTestSuitesFinished := NumberOfTestSuitesFinished + 1;
            ELSE
                GVL_TcUnit.CurrentTestSuiteBeingCalled := GVL_TcUnit.TestSuiteAddresses[Counter];
				GVL_TcUnit.CurrentTestSuiteBeingCalled^.SetStartedAtIfNotSet(Timestamp := F_GetCpuCounterAs64bit(GVL_TcUnit.GetCpuCounter));
                GVL_TcUnit.CurrentTestSuiteBeingCalled^();
				// This is not an efficient way to set the duration
				IF GVL_TcUnit.TestSuiteAddresses[Counter]^.AreAllTestsFinished() THEN
					GVL_TcUnit.CurrentTestSuiteBeingCalled^.CalculateDuration(FinishedAt := F_GetCpuCounterAs64bit(GVL_TcUnit.GetCpuCounter));
				END_IF
            END_IF
        END_FOR
        (* Check if some event triggered an abortion of running the tests, in that case abort it now.
           This can be accomplished by setting the result of the test suites run to finished. *)
        IF AbortRunningTestSuites THEN
            NumberOfTestSuitesFinished := GVL_TcUnit.NumberOfInitializedTestSuites;
        END_IF

        IF NumberOfTestSuitesFinished = GVL_TcUnit.NumberOfInitializedTestSuites THEN
            AllTestSuitesFinished := TRUE;
        END_IF
    END_IF
END_IF

// Store test suite results continuously
TestResults();

// Log test suite results continuously
TestResultLogger.LogTestSuiteResults();

// Publish the xUnit Xml file once if enabled
XmlTestResultPublisher.LogTestSuiteResults();

// Run the buffered ADS logger
GVL_TcUnit.AdsMessageQueue();
RunTestSuiteTestsInSequence INTERNAL

This runs all the test suites in sequence (one after the other)

Parameters

Name Type Description
TimeBetweenTestSuitesExecution TIME Time delay between a test suite is finished and the next test suite starts
Implementation
IF GVL_TcUnit.StartedAt = 0 THEN
    GVL_TcUnit.StartedAt := F_GetCpuCounterAs64bit(GVL_TcUnit.GetCpuCounter);
END_IF

TimerBetweenExecutionOfTestSuites(PT := TimeBetweenTestSuitesExecution);
// Run TcUnit test suites
IF NOT AllTestSuitesFinished THEN
    IF GVL_TcUnit.NumberOfInitializedTestSuites = 0 THEN
        AllTestSuitesFinished := TRUE;
    ELSIF GVL_TcUnit.NumberOfInitializedTestSuites > 0 THEN
        IF TimerBetweenExecutionOfTestSuites.Q THEN
            TimerBetweenExecutionOfTestSuites.IN := FALSE;
        END_IF
        IF GVL_TcUnit.TestSuiteAddresses[CurrentlyRunningTestSuite]^.AreAllTestsFinished() THEN
            IF CurrentlyRunningTestSuite <> GVL_TcUnit.NumberOfInitializedTestSuites THEN
                NumberOfTestSuitesFinished := NumberOfTestSuitesFinished + 1;
                CurrentlyRunningTestSuite := CurrentlyRunningTestSuite + 1;
                TimerBetweenExecutionOfTestSuites.IN := TRUE;
				GVL_TcUnit.CurrentTestSuiteBeingCalled^.CalculateDuration(FinishedAt := F_GetCpuCounterAs64bit(GVL_TcUnit.GetCpuCounter));
            END_IF
        ELSIF NOT TimerBetweenExecutionOfTestSuites.Q THEN
            GVL_TcUnit.CurrentTestSuiteBeingCalled := GVL_TcUnit.TestSuiteAddresses[CurrentlyRunningTestSuite];
			GVL_TcUnit.CurrentTestSuiteBeingCalled^.SetStartedAtIfNotSet(Timestamp := F_GetCpuCounterAs64bit(GVL_TcUnit.GetCpuCounter));
            GVL_TcUnit.CurrentTestSuiteBeingCalled^();
			// This is not an efficient way to set the duration
			IF GVL_TcUnit.TestSuiteAddresses[CurrentlyRunningTestSuite]^.AreAllTestsFinished() THEN
				GVL_TcUnit.CurrentTestSuiteBeingCalled^.CalculateDuration(FinishedAt := F_GetCpuCounterAs64bit(GVL_TcUnit.GetCpuCounter));
			END_IF
        END_IF
        (* Check if some event triggered an abortion of running the tests, in that case abort it now.
           This can be accomplished by setting the result of the test suites run to finished. *)
        IF AbortRunningTestSuites THEN
            NumberOfTestSuitesFinished := GVL_TcUnit.NumberOfInitializedTestSuites;
        END_IF
        IF NumberOfTestSuitesFinished = GVL_TcUnit.NumberOfInitializedTestSuites THEN
            AllTestSuitesFinished := TRUE;
        END_IF
    END_IF
END_IF

// Store test suite results continuously
TestResults();

// Log test suite results continuously
TestResultLogger.LogTestSuiteResults();

// Publish the xUnit Xml file once if enabled
XmlTestResultPublisher.LogTestSuiteResults();

// Run the buffered ADS logger
GVL_TcUnit.AdsMessageQueue();

Used by

Declaration source
// This function block is responsible for holding track of the tests and executing them.
FUNCTION_BLOCK FB_TcUnitRunner
VAR
    // Indication of whether all test suites have reported that they are finished
    AllTestSuitesFinished : BOOL := FALSE;
    AllTestSuitesFinishedTrigger : R_TRIG;

    //Test result information
    TestResults : FB_TestResults;

    (* Prints the results to ADS so that Visual Studio can display the results.
       This test result formatter can be replaced with something else than ADS *)
    AdsTestResultLogger : FB_AdsTestResultLogger(TestResults);
    TestResultLogger : I_TestResultLogger := AdsTestResultLogger;

    (* If this flag is set, it means that some external event triggered the
       request to abort running the test suites *)
    AbortRunningTestSuites : BOOL;

    // Publishes a xUnit compatible XML file
    xUnitXmlPublisher : FB_xUnitXmlPublisher(TestResults);
    XmlTestResultPublisher : I_TestResultLogger := xUnitXmlPublisher;
END_VAR