Set Tags - Can it be used as a counter?

Hi @stevereiss

Would be worth sharing how you increment Query Failed / Query Passed? but I feel as if this may be what you question on the how-to, apologies if I’m mistaken.

This is a tricky one (arguably the report shows PASS and FAIL anyways), but as Tags cant be incremented dynamically for each test (at least without additional code thereafter Id suspect), and Tags are test-level which means you could go as far as setting a Tag against all at the Setting level, or apply them during test execution or at the start, and Tags aren’t accessible at a suite level which limits how you could handle/try to approach this, so you are limited to test case/test teardown for an approach without any additional code thereafter.

So my initial idea, just from a glance was as below:
Create a keyword that checks the test status after it’s completed by using test teardown or extend your current ones (test teardown is executed after the test case has been executed) and increment a suite or global variable ${COUNT_PASSED} / ${COUNT_FAILED}, and then apply a tag for name/count for whichever status PASS/FAIL it falls into via the Set Tag keyword, for which you can make use an Automatic variable ${TEST STATUS} within, which would be the automatic variable ${TEST_STATUS} to check for the status with a simple IF block, and thereafter use a Suite Teardown to remove all minus the uppermost value so only that shows on the report, but as you cant use retrieve @{TEST TAGS} in suite level or use them at this level, this cannot be done in what I theorized as an approach entirely.

So similar to the approach above, you could create a “dud” test that is excluded from the count but applies the Tags thereafter, and you’d be able to order these in a way that this would be the last test ran see How to execute tests in sequential order in robot framework? or similar on the forum

The only downside is a there be a test that’s more of a task shown in the report, possibly this could be excluded without losing the tag count

So an approach could be as below:

*** Settings ***
Resource             keywords.resource
Resource             variables.resource
Test Teardown        Test_Outcome_Check_Status 

*** Variables ***
${COUNT_PASSED}   ${0} 
${COUNT_FAILED}   ${0}

*** Keywords ***
Test_Outcome_Check_Status 
    IF    "${TEST STATUS}" == "PASS" and "${TEST_NAME}" != "Last Test To Assign Tags"
        ${count}=    Evaluate         ${COUNT_PASSED} + 1  
        Set Global Variable       ${COUNT_PASSED}      ${count}   
    ELSE IF  "${TEST STATUS}" == "FAIL" and "${TEST_NAME}" != "Last Test To Assign Tags"
        ${count}=    Evaluate    ${COUNT_FAILED}+1
        Set Global Variable       ${COUNT_FAILED}      ${count}        
    END
    
    IF    "${TEST_NAME}" == "Last Test To Assign Tags"
        Set Tags   Query Passed ${COUNT_PASSED}
        Set Tags   Query Failed ${COUNT_FAILED} 
    END


*** Test Cases ***
Test Case 1
    # Force fail
    Fail    Test Failed

Test Case 2
    # PASSED
    No Operation

Test Case 3
    # PASSED
    No Operation

Last Test To Assign Tags
    # Will do nothing other than as a pointer to set tags as the last test in execution
    No Operation

Outcome shows below:

I’m sure others with more experience could give other approaches, hope that helps nonetheless :slight_smile: but this isn’t something I’ve needed to do or probably will need to do personally, but was a fun one to have a stab at keeping within what RF gives out the box

1 Like