Skip to content

Commit dbc7cd0

Browse files
authored
Merge pull request #2 from vearne/develop
add subcommand extract
2 parents 0b3c598 + 8cce018 commit dbc7cd0

File tree

6 files changed

+106
-9
lines changed

6 files changed

+106
-9
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ LDFLAGS = -ldflags "-s -w"
22

33
.PHONY: build
44
build:
5-
env go build ${LDFLAGS} -o autotest ./
5+
env CGO_ENABLED=0 go build ${LDFLAGS} -o autotest ./
66
xgo:
77
xgo -out=./autotest .

README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@ Automated testing framework for api services, http, gRPC (may support in the fut
1414
## Usage
1515
### 1) check configuration file
1616
```
17-
autotest --config-file=${CONFIG_FILE}
17+
autotest test --config-file=${CONFIG_FILE}
1818
```
1919

2020
### 2) execute automated tests
2121
```
22-
autotest --config-file=${CONFIG_FILE} --env-file=${ENV_FILE}
22+
autotest run --config-file=${CONFIG_FILE} --env-file=${ENV_FILE}
2323
```
24+
### 3) extract the value corresponding to xpath
25+
```
26+
autotest extract --xpath=${XPATH} --json=${JSON}
27+
```
28+
2429
## Example
2530
### 1) start a fake http api service
2631
```
@@ -56,3 +61,20 @@ curl 'http://localhost:8080/api/books'
5661
make build
5762
./autotest run -c=./config_files/autotest.yml -e=./config_files/.env.dev
5863
```
64+
65+
### 3) extract the value corresponding to xpath
66+
get the title of each book in the book list
67+
```
68+
./autotest extract -x "//title" -j '[
69+
{
70+
"id": 2,
71+
"title": "Effective Go",
72+
"author": "The Go Authors"
73+
},
74+
{
75+
"id": 3,
76+
"title": "book3_title",
77+
"author": "book3_author-2"
78+
}
79+
]'
80+
```

README_zh.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@
1212
## 用法
1313
### 1) 检查配置文件
1414
```
15-
autotest --config-file=${CONFIG_FILE}
15+
autotest test --config-file=${CONFIG_FILE}
1616
```
1717

1818
### 2) 执行自动化测试
1919
```
20-
autotest --config-file=${CONFIG_FILE} --env-file=${ENV_FILE}
20+
autotest run --config-file=${CONFIG_FILE} --env-file=${ENV_FILE}
21+
```
22+
23+
### 3) 提取xpath对应的值
24+
```
25+
autotest extract --xpath=${XPATH} --json=${JSON}
2126
```
2227

2328
## 示例
@@ -55,3 +60,20 @@ make build
5560
./autotest run -c=./config_files/autotest.yml -e=./config_files/.env.dev
5661
```
5762

63+
### 3) 提取xpath对应的值
64+
获取书本列表中,书的title
65+
```
66+
./autotest extract -x "//title" -j '[
67+
{
68+
"id": 2,
69+
"title": "Effective Go",
70+
"author": "The Go Authors"
71+
},
72+
{
73+
"id": 3,
74+
"title": "book3_title",
75+
"author": "book3_author-2"
76+
}
77+
]'
78+
```
79+

internal/command/command.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package command
22

33
import (
44
"context"
5+
"github.com/antchfx/jsonquery"
56
"github.com/antchfx/xpath"
67
"github.com/urfave/cli/v3"
78
"github.com/vearne/autotest/internal/resource"
89
"github.com/vearne/autotest/internal/rule"
910
slog "github.com/vearne/simplelog"
1011
"github.com/vearne/zaplog"
12+
"strings"
1113
)
1214

1315
func RunTestCases(ctx context.Context, cmd *cli.Command) error {
@@ -57,6 +59,7 @@ func ValidateConfig(ctx context.Context, cmd *cli.Command) error {
5759
}
5860

5961
slog.Info("=== validate config file ===")
62+
slog.Info("1. check xpath")
6063
for filePath, testcases := range resource.HttpTestCases {
6164
slog.Info("filePath:%v, len(testcases):%v", filePath, len(testcases))
6265
for _, tc := range testcases {
@@ -70,7 +73,7 @@ func ValidateConfig(ctx context.Context, cmd *cli.Command) error {
7073
return err
7174
}
7275
case "HttpBodyAtLeastOneRule":
73-
rule := r.(*rule.HttpBodyEqualRule)
76+
rule := r.(*rule.HttpBodyAtLeastOneRule)
7477
_, err := xpath.Compile(rule.Xpath)
7578
if err != nil {
7679
slog.Error("rule error, testCaseId:%v, xpath:%v", tc.ID, rule.Xpath)
@@ -82,5 +85,48 @@ func ValidateConfig(ctx context.Context, cmd *cli.Command) error {
8285
}
8386
}
8487
}
88+
89+
slog.Info("2. check if ID is duplicate")
90+
for filePath, testcases := range resource.HttpTestCases {
91+
slog.Info("filePath:%v, len(testcases):%v", filePath, len(testcases))
92+
exist := make(map[uint64]struct{})
93+
for _, tc := range testcases {
94+
_, ok := exist[tc.ID]
95+
if ok {
96+
slog.Error("filePath:%v, ID [%v] is duplicate", filePath, tc.ID)
97+
break
98+
}
99+
exist[tc.ID] = struct{}{}
100+
}
101+
}
102+
return nil
103+
}
104+
105+
func ExtractXpath(ctx context.Context, cmd *cli.Command) error {
106+
// 检查testcase的xpath语法是否正确
107+
108+
xpathStr := cmd.String("xpath")
109+
slog.Info("xpathStr:%v", xpathStr)
110+
111+
jsonStr := cmd.String("json")
112+
slog.Info("jsonStr:%v", jsonStr)
113+
114+
_, err := xpath.Compile(xpathStr)
115+
if err != nil {
116+
slog.Error("xpath syntax error")
117+
return nil
118+
}
119+
120+
doc, err := jsonquery.Parse(strings.NewReader(jsonStr))
121+
if err != nil {
122+
slog.Error("jsonStr format error")
123+
return nil
124+
}
125+
nodes := jsonquery.Find(doc, xpathStr)
126+
for idx, node := range nodes {
127+
if node != nil {
128+
slog.Info("[%v] = %v", idx, node.Value())
129+
}
130+
}
85131
return nil
86132
}

internal/rule/http_rule.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package rule
22

33
import (
4-
"fmt"
54
"github.com/antchfx/jsonquery"
65
"github.com/go-resty/resty/v2"
76
"strings"
@@ -58,7 +57,6 @@ func (r *HttpBodyAtLeastOneRule) Verify(resp *resty.Response) bool {
5857
nodes := jsonquery.Find(doc, r.Xpath)
5958
for _, node := range nodes {
6059
if node != nil && convStr(r.Expected) == convStr(node.Value()) {
61-
fmt.Printf("xpath:%v, value:%v\n", r.Xpath, node.Value())
6260
return true
6361
}
6462
}

main.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
const (
14-
version = "v0.0.1"
14+
version = "v0.0.2"
1515
)
1616

1717
func main() {
@@ -45,6 +45,15 @@ func main() {
4545
Usage: "run all test cases",
4646
Action: command.RunTestCases,
4747
},
48+
{
49+
Name: "extract",
50+
Flags: []cli.Flag{
51+
&cli.StringFlag{Name: "xpath", Aliases: []string{"x"}},
52+
&cli.StringFlag{Name: "json", Aliases: []string{"j"}},
53+
},
54+
Usage: "try to extract data corresponding to xpath from json string",
55+
Action: command.ExtractXpath,
56+
},
4857
},
4958
}
5059

0 commit comments

Comments
 (0)