I am running a script file on CircleCI where I do my UI unit tests and I need to parse the resulting XML from android and then find the specific tests that succeeded put them in a list and then take the failures and put them in another list. I will then iterate through both lists and trigger API calls for each updating our Test Cases with the results. One test equals one Test Case ticket. In my bash script I have this so far:
totalTests=$(xmllint --xpath 'count(//testsuite/testcase)' $xml_file)
echo "Total tests = $totalTests"
# This is where I need help how to find the and grab the tag that has child tag of failure
failures=$(xmllint --xpath '//testsuite/testcase/????' $xml_file)
echo "failures=$failures"
# This is where I need help how to find the and grab the tag that has no child tag of failure
successes1=$(xmllint --xpath '//testsuite/testcase/@name' $xml_file)
echo "successes1=$successes1"
IFS="$(printf '\nx')" && IFS="${IFS%x}";
for entry in $successes1
do
number=`echo $entry | awk -F_ '{print $2}'`
echo "CASE-$number"
done
...
# for loop for the failures ....
...
My test case out put looks like this
<?xml version='1.0' encoding='UTF-8' ?>
<testsuite name="com.android.OnboardingBottomNavigation" tests="3" failures="1" errors="0" skipped="0" time="27.017" timestamp="2023-08-30T16:37:38" hostname="localhost">
<properties>
<property name="device" value="Pixel_4_API_33(AVD) - 13" />
<property name="flavor" value="" />
<property name="project" value=":app" />
</properties>
<testcase name="CASE_11447_Skip_Onboarding_Click_All_BottomNavs" classname="com.android.OnboardingBottomNavigation" time="5.483">
<failure>java.lang.AssertionError: Expected value to be true.
at org.junit.Assert.fail(Assert.java:89)
at kotlin.test.junit.JUnitAsserter.fail(JUnitSupport.kt:56)
at kotlin.test.Asserter$DefaultImpls.assertTrue(Assertions.kt:648)
at kotlin.test.junit.JUnitAsserter.assertTrue(JUnitSupport.kt:30)
at kotlin.test.Asserter$DefaultImpls.assertTrue(Assertions.kt:658)
at kotlin.test.junit.JUnitAsserter.assertTrue(JUnitSupport.kt:30)
at kotlin.test.AssertionsKt__AssertionsKt.assertTrue(Assertions.kt:44)
at kotlin.test.AssertionsKt.assertTrue(Unknown Source:1)
at kotlin.test.AssertionsKt__AssertionsKt.assertTrue$default(Assertions.kt:42)
at kotlin.test.AssertionsKt.assertTrue$default(Unknown Source:1)
at com.android.OnboardingBottomNavigation.CASE_11447_Skip_Onboarding_Click_All_BottomNavs(OnboardingBottomNavigation.kt:39)</failure>
</testcase>
<testcase name="CASE_11445_Skip_Onboarding_Click_All_BottomNavs" classname="com.android.OnboardingBottomNavigation" time="15.353" />
<testcase name="CASE_11446_Skip_Onboarding_Click_All_BottomNavs" classname="com.android.OnboardingBottomNavigation" time="2.513" />
</testsuite>
You can make a subquery in brackets which can evaluate to true:
xmllint --xpath '/testsuite/testcase[failure]'
<testcase name="CASE_11447_Skip_Onboarding_Click_All_BottomNavs" classname="com.android.OnboardingBottomNavigation" time="5.483">
<failure>java.lang.AssertionError: Expected value to be true.
at org.junit.Assert.fail(Assert.java:89)
at kotlin.test.junit.JUnitAsserter.fail(JUnitSupport.kt:56)
at kotlin.test.Asserter$DefaultImpls.assertTrue(Assertions.kt:648)
at kotlin.test.junit.JUnitAsserter.assertTrue(JUnitSupport.kt:30)
at kotlin.test.Asserter$DefaultImpls.assertTrue(Assertions.kt:658)
at kotlin.test.junit.JUnitAsserter.assertTrue(JUnitSupport.kt:30)
at kotlin.test.AssertionsKt__AssertionsKt.assertTrue(Assertions.kt:44)
at kotlin.test.AssertionsKt.assertTrue(Unknown Source:1)
at kotlin.test.AssertionsKt__AssertionsKt.assertTrue$default(Assertions.kt:42)
at kotlin.test.AssertionsKt.assertTrue$default(Unknown Source:1)
at com.android.OnboardingBottomNavigation.CASE_11447_Skip_Onboarding_Click_All_BottomNavs(OnboardingBottomNavigation.kt:39)</failure>
</testcase>
And count
it:
xmllint --xpath 'count(/testsuite/testcase[failure])'
1
Or, you can negate it with not
:
xmllint --xpath '/testsuite/testcase[not(failure)]'
<testcase name="CASE_11445_Skip_Onboarding_Click_All_BottomNavs" classname="com.android.OnboardingBottomNavigation" time="15.353"/>
<testcase name="CASE_11446_Skip_Onboarding_Click_All_BottomNavs" classname="com.android.OnboardingBottomNavigation" time="2.513"/>
And count
it:
xmllint --xpath 'count(/testsuite/testcase[failure])'
2