-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support to generate robot-framework script (#563)
Co-authored-by: rick <[email protected]>
- Loading branch information
1 parent
33703da
commit 0f02984
Showing
18 changed files
with
298 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
+++ | ||
title = "代码生成" | ||
+++ | ||
|
||
`atest` 支持把测试用例生成多种开发语言的代码: | ||
|
||
* curl | ||
* Java | ||
* Golang | ||
* Python | ||
* JavaScript | ||
* [Robot Framework](https://robotframework.org/) | ||
|
||
> 该功能需要在 Web UI 上使用。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
export sourcefile=$1 | ||
# exit if no source file is specified | ||
if [ -z "$sourcefile" ] | ||
then | ||
echo "no source file is specified" | ||
exit 1 | ||
fi | ||
|
||
mv ${sourcefile} test.robot | ||
pip install robotframework robotframework-requests | ||
robot test.robot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
*** Settings *** | ||
Library RequestsLibrary | ||
|
||
*** Test Cases *** | ||
{{- range $item := .Items}} | ||
{{$item.Name}} | ||
{{- if $item.Request.Header}} | ||
${headers}= Create Dictionary {{- range $key, $val := $item.Request.Header}} {{$key}} {{$val}}{{- end}} | ||
{{- end}} | ||
${response}= {{$item.Request.Method}} {{$item.Request.API}}{{- if .Request.Header}} headers=${headers}{{end}} | ||
{{- end}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
*** Settings *** | ||
Library RequestsLibrary | ||
|
||
*** Test Cases *** | ||
{{.Name}} | ||
{{- if .Request.Header}} | ||
${headers}= Create Dictionary {{- range $key, $val := .Request.Header}} {{$key}} {{$val}}{{- end}} | ||
{{- end}} | ||
${response}= {{.Request.Method}} {{.Request.API}}{{- if .Request.Header}} headers=${headers}{{end}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
Copyright 2024 API Testing Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package generator | ||
|
||
import ( | ||
_ "embed" | ||
|
||
"github.com/linuxsuren/api-testing/pkg/testing" | ||
) | ||
|
||
type robotGenerator struct { | ||
} | ||
|
||
func NewRobotGenerator() CodeGenerator { | ||
return &robotGenerator{} | ||
} | ||
|
||
func (g *robotGenerator) Generate(testSuite *testing.TestSuite, testcase *testing.TestCase) (string, error) { | ||
tpl := robotTemplate | ||
if testcase == nil { | ||
tpl = robotSuiteTemplate | ||
} | ||
return generate(testSuite, testcase, "robot-framework", tpl) | ||
} | ||
|
||
func init() { | ||
RegisterCodeGenerator("robot-framework", NewRobotGenerator()) | ||
} | ||
|
||
//go:embed data/robot.tpl | ||
var robotTemplate string | ||
|
||
//go:embed data/robot-suite.tpl | ||
var robotSuiteTemplate string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
Copyright 2024 API Testing Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package generator | ||
|
||
import ( | ||
_ "embed" | ||
"testing" | ||
|
||
atest "github.com/linuxsuren/api-testing/pkg/testing" | ||
) | ||
|
||
func TestRobotGenerator(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
testCase *atest.TestCase | ||
testSuite *atest.TestSuite | ||
expect string | ||
}{{ | ||
name: "simple", | ||
testCase: &atest.TestCase{ | ||
Name: "simple", | ||
Request: atest.Request{ | ||
API: fooForTest, | ||
}, | ||
}, | ||
expect: simpleRobot, | ||
}, { | ||
name: "with header", | ||
testCase: &atest.TestCase{ | ||
Name: "simple", | ||
Request: atest.Request{ | ||
API: fooForTest, | ||
Header: map[string]string{ | ||
"key": "value", | ||
}, | ||
}, | ||
}, | ||
expect: headerRobot, | ||
}, { | ||
name: "test suite", | ||
testSuite: &atest.TestSuite{ | ||
Items: []atest.TestCase{{ | ||
Name: "one", | ||
Request: atest.Request{ | ||
API: fooForTest, | ||
Header: map[string]string{ | ||
"key1": "value1", | ||
}, | ||
}, | ||
}, { | ||
Name: "two", | ||
Request: atest.Request{ | ||
API: fooForTest, | ||
Header: map[string]string{ | ||
"key2": "value2", | ||
}, | ||
}, | ||
}}, | ||
}, | ||
expect: suiteRobot, | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := NewRobotGenerator() | ||
if got, err := g.Generate(tt.testSuite, tt.testCase); err != nil || got != tt.expect { | ||
t.Errorf("got %q, want %q, error: %v", got, tt.expect, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
//go:embed testdata/simple.robot | ||
var simpleRobot string | ||
|
||
//go:embed testdata/with-headers.robot | ||
var headerRobot string | ||
|
||
//go:embed testdata/suite.robot | ||
var suiteRobot string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
*** Settings *** | ||
Library RequestsLibrary | ||
|
||
*** Test Cases *** | ||
simple | ||
${response}= GET http://foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
*** Settings *** | ||
Library RequestsLibrary | ||
|
||
*** Test Cases *** | ||
one | ||
${headers}= Create Dictionary key1 value1 | ||
${response}= GET http://foo headers=${headers} | ||
two | ||
${headers}= Create Dictionary key2 value2 | ||
${response}= GET http://foo headers=${headers} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
*** Settings *** | ||
Library RequestsLibrary | ||
|
||
*** Test Cases *** | ||
simple | ||
${headers}= Create Dictionary key value | ||
${response}= GET http://foo headers=${headers} |
Oops, something went wrong.