Skip to content

Commit 0f02984

Browse files
authored
feat: support to generate robot-framework script (#563)
Co-authored-by: rick <[email protected]>
1 parent 33703da commit 0f02984

File tree

18 files changed

+298
-45
lines changed

18 files changed

+298
-45
lines changed

cmd/run.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type runOption struct {
5151
duration time.Duration
5252
requestTimeout time.Duration
5353
requestIgnoreError bool
54-
caseFilter string
54+
caseFilter []string
5555
thread int64
5656
context context.Context
5757
qps int32
@@ -117,7 +117,7 @@ See also https://github.com/LinuxSuRen/api-testing/tree/master/sample`,
117117
flags.DurationVarP(&opt.duration, "duration", "", 0, "Running duration")
118118
flags.DurationVarP(&opt.requestTimeout, "request-timeout", "", time.Minute, "Timeout for per request")
119119
flags.BoolVarP(&opt.requestIgnoreError, "request-ignore-error", "", false, "Indicate if ignore the request error")
120-
flags.StringVarP(&opt.caseFilter, "case-filter", "", "", "The filter of the test case")
120+
flags.StringArrayVarP(&opt.caseFilter, "case-filter", "", nil, "The filter of the test case")
121121
flags.StringVarP(&opt.report, "report", "", "", "The type of target report. Supported: markdown, md, html, json, discard, std, prometheus, http, grpc")
122122
flags.StringVarP(&opt.reportFile, "report-file", "", "", "The file path of the report")
123123
flags.BoolVarP(&opt.reportIgnore, "report-ignore", "", false, "Indicate if ignore the report output")
@@ -359,8 +359,20 @@ func (o *runOption) runSuite(loader testing.Loader, dataContext map[string]inter
359359
}
360360
runLogger.Info("run test suite", "name", testSuite.Name, "filter", caseFilter)
361361
for _, testCase := range testSuite.Items {
362-
if caseFilterObj != nil && !strings.Contains(testCase.Name, caseFilterObj.(string)) {
363-
continue
362+
if caseFilterObj != nil {
363+
if filter, ok := caseFilterObj.([]string); ok && len(filter) > 0{
364+
match := false
365+
for _, ff := range filter {
366+
if strings.Contains(testCase.Name, ff) {
367+
match = true
368+
break
369+
}
370+
}
371+
372+
if !match {
373+
continue
374+
}
375+
}
364376
}
365377
if !testCase.InScope(o.caseItems) {
366378
continue
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
+++
2+
title = "代码生成"
3+
+++
4+
5+
`atest` 支持把测试用例生成多种开发语言的代码:
6+
7+
* curl
8+
* Java
9+
* Golang
10+
* Python
11+
* JavaScript
12+
* [Robot Framework](https://robotframework.org/)
13+
14+
> 该功能需要在 Web UI 上使用。

e2e/code-generator/compose.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ services:
2626
command:
2727
- /workspace/entrypoint.sh
2828
- python
29+
robot-framework:
30+
build:
31+
context: .
32+
dockerfile: Dockerfile
33+
args:
34+
- LAN_ENV=docker.io/library/python:3.8
35+
command:
36+
- /workspace/entrypoint.sh
37+
- robot-framework
2938
javascript:
3039
build:
3140
context: .
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
set -e
3+
4+
export sourcefile=$1
5+
# exit if no source file is specified
6+
if [ -z "$sourcefile" ]
7+
then
8+
echo "no source file is specified"
9+
exit 1
10+
fi
11+
12+
mv ${sourcefile} test.robot
13+
pip install robotframework robotframework-requests
14+
robot test.robot

e2e/code-generator/start.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ set -e
33

44
docker compose version
55

6-
targets=(golang java python javascript curl)
6+
targets=(golang java python javascript curl robot-framework)
77
for target in "${targets[@]}"
88
do
99
docker compose down

pkg/generator/code_generator.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ limitations under the License.
1515
*/
1616
package generator
1717

18-
import "github.com/linuxsuren/api-testing/pkg/testing"
18+
import (
19+
"bytes"
20+
"fmt"
21+
"net/http"
22+
"text/template"
23+
24+
"github.com/linuxsuren/api-testing/pkg/testing"
25+
)
1926

2027
// CodeGenerator is the interface of code generator
2128
type CodeGenerator interface {
@@ -64,3 +71,35 @@ func GetTestSuiteConverters() (result map[string]TestSuiteConverter) {
6471
}
6572
return
6673
}
74+
75+
func generate(testsuite *testing.TestSuite, testcase *testing.TestCase, templateName, templateText string) (result string, err error) {
76+
if testcase != nil && testcase.Request.Method == "" {
77+
testcase.Request.Method = http.MethodGet
78+
}
79+
if testsuite != nil && testsuite.Items != nil {
80+
for i, _ := range testsuite.Items {
81+
if testsuite.Items[i].Request.Method == "" {
82+
testsuite.Items[i].Request.Method = http.MethodGet
83+
}
84+
}
85+
}
86+
var tpl *template.Template
87+
if tpl, err = template.New(templateName).
88+
Funcs(template.FuncMap{"safeString": safeString}).
89+
Parse(templateText); err == nil {
90+
buf := new(bytes.Buffer)
91+
var ctx interface{}
92+
if testcase == nil {
93+
ctx = testsuite
94+
} else {
95+
ctx = testcase
96+
}
97+
98+
if err = tpl.Execute(buf, ctx); err == nil {
99+
result = buf.String()
100+
}
101+
} else {
102+
fmt.Println(err)
103+
}
104+
return
105+
}

pkg/generator/data/robot-suite.tpl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
*** Settings ***
2+
Library RequestsLibrary
3+
4+
*** Test Cases ***
5+
{{- range $item := .Items}}
6+
{{$item.Name}}
7+
{{- if $item.Request.Header}}
8+
${headers}= Create Dictionary {{- range $key, $val := $item.Request.Header}} {{$key}} {{$val}}{{- end}}
9+
{{- end}}
10+
${response}= {{$item.Request.Method}} {{$item.Request.API}}{{- if .Request.Header}} headers=${headers}{{end}}
11+
{{- end}}

pkg/generator/data/robot.tpl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*** Settings ***
2+
Library RequestsLibrary
3+
4+
*** Test Cases ***
5+
{{.Name}}
6+
{{- if .Request.Header}}
7+
${headers}= Create Dictionary {{- range $key, $val := .Request.Header}} {{$key}} {{$val}}{{- end}}
8+
{{- end}}
9+
${response}= {{.Request.Method}} {{.Request.API}}{{- if .Request.Header}} headers=${headers}{{end}}

pkg/generator/javascript_generator.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ limitations under the License.
1616
package generator
1717

1818
import (
19-
"bytes"
20-
"net/http"
21-
"text/template"
22-
2319
_ "embed"
2420

2521
"github.com/linuxsuren/api-testing/pkg/testing"
@@ -32,18 +28,8 @@ func NewJavaScriptGenerator() CodeGenerator {
3228
return &javascriptGenerator{}
3329
}
3430

35-
func (g *javascriptGenerator) Generate(testSuite *testing.TestSuite, testcase *testing.TestCase) (result string, err error) {
36-
if testcase.Request.Method == "" {
37-
testcase.Request.Method = http.MethodGet
38-
}
39-
var tpl *template.Template
40-
if tpl, err = template.New("javascript template").Funcs(template.FuncMap{"safeString": safeString}).Parse(javascriptTemplate); err == nil {
41-
buf := new(bytes.Buffer)
42-
if err = tpl.Execute(buf, testcase); err == nil {
43-
result = buf.String()
44-
}
45-
}
46-
return
31+
func (g *javascriptGenerator) Generate(testSuite *testing.TestSuite, testcase *testing.TestCase) (string, error) {
32+
return generate(testSuite, testcase, "javascript template", javascriptTemplate)
4733
}
4834

4935
func init() {

pkg/generator/python_generator.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ limitations under the License.
1616
package generator
1717

1818
import (
19-
"bytes"
20-
"net/http"
21-
"text/template"
22-
2319
_ "embed"
2420

2521
"github.com/linuxsuren/api-testing/pkg/testing"
@@ -33,17 +29,7 @@ func NewPythonGenerator() CodeGenerator {
3329
}
3430

3531
func (g *pythonGenerator) Generate(testSuite *testing.TestSuite, testcase *testing.TestCase) (result string, err error) {
36-
if testcase.Request.Method == "" {
37-
testcase.Request.Method = http.MethodGet
38-
}
39-
var tpl *template.Template
40-
if tpl, err = template.New("python template").Parse(pythonTemplate); err == nil {
41-
buf := new(bytes.Buffer)
42-
if err = tpl.Execute(buf, testcase); err == nil {
43-
result = buf.String()
44-
}
45-
}
46-
return
32+
return generate(testSuite, testcase, "python template", pythonTemplate)
4733
}
4834

4935
func init() {

0 commit comments

Comments
 (0)