在 Bash 脚本中使用 XMLLINT 解析 XML 单元测试文件,并将成功和失败放入数组中

问题描述 投票:0回答:1

我正在 CircleCI 上运行一个脚本文件,我在其中进行 UI 单元测试,我需要解析来自 android 的结果 XML,然后找到成功的特定测试,将它们放入列表中,然后将失败的测试放入另一个列表中。然后,我将迭代这两个列表并触发 API 调用,以便用结果更新我们的测试用例。一项测试等于一张测试用例票。到目前为止,在我的 bash 脚本中我有这个:

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 ....
...

我的测试用例看起来像这样

<?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>
android bash unit-testing android-espresso xmllint
1个回答
0
投票

您可以在括号中创建子查询,其计算结果为 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>

还有

count
它:

xmllint --xpath 'count(/testsuite/testcase[failure])'
1

或者,你可以用

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"/>

还有

count
它:

xmllint --xpath 'count(/testsuite/testcase[failure])'
2
© www.soinside.com 2019 - 2024. All rights reserved.