我有一个junit文件。如何从testt获得号码?
如果上面一行包含"/system- error "或"/failure"然后测试失败,写入失败id为test的文件。
如果上面一行包含"/system-out"则测试成功,写入通过的文件id test。
例子"/system-err"
<system-err>Assertion Failure at Navigation.swift:88:Failed to get matching snapshot: No matches found for first query match sequence: `Descendants matching type NavigationBar`, given input App element pid: 21141 (no attribute values faulted in)</system-err>
<system-out>testIT: C477997</system-out>
例子"/failure"
> <failure message='Assertion Failure at Element.swift:277:Failed to get matching snapshot: No matches found for Elements matching
> predicate '"One" IN identifiers' from input {(
> Button, identifier: 'dictation', label: 'Диктовать', Disabled,
> Button, label: 'Почему мне не приходит СМС?',
> Button, label: 'Назад',
> Button,
> Button, label: 'Продолжить'
> )}'>
> </failure>
> <system-out>testIT: C478067</system-out>
通过了测试
<system-out> Setting up automation session</system-out>
<system-out>testIT: C478057</system-out>
失败文件包含id
477997
478067
传递的文件包含id
478057
我试着
xmllint $1 | awk -F'testIT: C|, | \(' '/testIT:/{print $2"n"$4"n"$6}'| awk 'NF!=0' | sort| uniq| 2>/dev/null > successfullCases.xml
但是我得到
470836</system-out>
477975</system-out>
477997</system-out>
478057</system-out>
478067</system-out>
478098</system-out>
完整文件https://disk.yandex.ru/d/DhdlvwfYj4J00A
让我们试试:
arr=(`grep -En "C[0-9]{6}" $1 | cut -d":" -f 1 | xargs`)
>fail.out
>err.out
>pass.out
for i in ${!arr[@]}; do
line_num="${arr[$i]}"
if head -$line_num $1 | tail -2 | grep -q "/system-err"
then
echo -ne "$i) Line: $line_numttErrorttID: "
head -$line_num $1 | tail -1 | grep -Eo '[0-9]{6}'
head -$line_num $1 | tail -1 | grep -Eo '[0-9]{6}' >> err.out
#--------------------------------------------------------
elif head -$line_num $1 | tail -2 | grep -q "/failure"
then
echo -ne "$i) Line: $line_numttFailurettID: "
head -$line_num $1 | tail -1 | grep -Eo '[0-9]{6}'
head -$line_num $1 | tail -1 | grep -Eo '[0-9]{6}' >> fail.out
# ----------------------------------------------------------
elif head -$line_num $1 | tail -2 | grep -q "/system-out"
then
echo -ne "$i) Line: $line_numttPassttID: "
head -$line_num $1 | tail -1 | grep -Eo '[0-9]{6}'
head -$line_num $1 | tail -1 | grep -Eo '[0-9]{6}' >> pass.out
# ----------------------------------------------------------
fi
done
将脚本保存在一个文件中,并尝试这样运行:
[user@host]# bash script.sh report.junit
试试这个:
grep -Eo '[0-9]{6}' file.txt
描述:
-E
或--extended-regexp
:将模式解释为扩展正则表达式
和
-o
或--only-matching
:只打印匹配行中匹配(非空)的部分,每个部分在单独的输出行上。
更多阅读:
- grep(1):打印行匹配模式- Linux手册页
注意:
同样,如果您想导出"error";pass"或者"失败",您应该编写一个脚本逐行处理您的输入文件。我可以帮你更多的忙。
假设您的输入类似于:
$ cat input_file
<system-err>Assertion Failure at Navigation.swift:88:Failed to get matching snapshot: No matches found for first query match sequence: `Descendants matching type NavigationBar`, given input App element pid: 21141 (no attribute values faulted in)</system-err>
<system-out>testIT: C477997</system-out>
> <failure message='Assertion Failure at Element.swift:277:Failed to get matching snapshot: No matches found for Elements matching
> predicate '"One" IN identifiers' from input {(
> Button, identifier: 'dictation', label: 'Диктовать', Disabled,
> Button, label: 'Почему мне не приходит СМС?',
> Button, label: 'Назад',
> Button,
> Button, label: 'Продолжить'
> )}'>
> </failure>
> <system-out>testIT: C478067</system-out>
<system-out> Setting up automation session</system-out>
<system-out>testIT: C478057</system-out>
使用sed
$ cat script.sed
N
s~.*/(system-err|failure).*n.*testIT[^0-9]*([0-9]+).*~2~pwfail.txt
s~.*/system-out.*n.*testIT[^0-9]*([0-9]+).*~1~pwpass.txt
p
您可以使用以下脚本:
$ sed -Enf script.sed input_file
或者作为一行
$ sed -Ene 'N;{s~.*/(system-err|failure).*n.*testIT[^0-9]*([0-9]+).*~2~pwfail.txt' -e 's~.*/system-out.*n.*testIT[^0-9]*([0-9]+).*~1~pwpass.txt' -e '}' input_file
$ head {pass,fail}.txt
==> pass.txt <==
478057
==> fail.txt <==
477997
478067