cli_co_test.go 10.1 KB
Newer Older
Z
zhaoke 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
package main

/**

$>ztf set                                        根据系统提示,设置语言、禅道地址、账号等,Windows下会提示输入语言解释程序。
$>ztf co                                         交互式导出禅道测试用例,将提示用户输入导出类型和编号。
$>ztf co -product 1 -language php             导出编号为1的产品测试用例,使用php语言,缩写-p -l。
$>ztf co -p 1 -m 15 -l php                    导出产品编号为1、模块编号为15的测试用例。
$>ztf co -s 1 -l php -i true                  导出编号为1的套件所含测试用例,期待结果保存在独立文件中。
$>ztf co -t 1 -l php                          导出编号为1的测试单所含用例。

*/
import (
	"fmt"
Z
zhaoke 已提交
15 16
	"os"
	"path"
Z
zhaoke 已提交
17
	"regexp"
Z
zhaoke 已提交
18
	"runtime"
Z
zhaoke 已提交
19
	"strconv"
Z
zhaoke 已提交
20
	"strings"
Z
zhaoke 已提交
21 22 23
	"testing"
	"time"

aaronchen2k2k's avatar
aaronchen2k2k 已提交
24 25
	commonTestHelper "github.com/easysoft/zentaoatf/cmd/test/helper/common"
	constTestHelper "github.com/easysoft/zentaoatf/cmd/test/helper/conf"
Z
zhaoke 已提交
26
	expect "github.com/easysoft/zentaoatf/pkg/lib/expect"
Z
zhaoke 已提交
27
	fileUtils "github.com/easysoft/zentaoatf/pkg/lib/file"
雨爱无痕 已提交
28 29
	"github.com/ozontech/allure-go/pkg/framework/provider"
	"github.com/ozontech/allure-go/pkg/framework/suite"
Z
zhaoke 已提交
30 31 32
)

var (
雨爱无痕 已提交
33
	coSuccessRe  = regexp.MustCompile("Success")
Z
zhaoke 已提交
34 35 36 37 38 39 40 41 42 43
	typeRe       = regexp.MustCompile("Import test cases from|请选择用例来源")
	productRe    = regexp.MustCompile("Please enter Product Id|请输入 产品Id")
	moduleRe     = regexp.MustCompile("Please enter Module Id|请输入 模块Id")
	suiteRe      = regexp.MustCompile("Please enter Suite Id|请输入 套件Id")
	taskRe       = regexp.MustCompile("Please enter Test Request Id|请输入 测试任务Id")
	separateRe   = regexp.MustCompile("Save expected results in a separate file|是否将用例期待结果保存在独立的文件中")
	storeRe      = regexp.MustCompile("Where to store scripts|请输入脚本保存目录")
	organizeRe   = regexp.MustCompile("Organize test scripts by module|是否希望按模块ID组织脚本目录结构")
	successCoRe  = regexp.MustCompile("Successfully generated \\d+ test scripts|成功创建\\d+个测试脚本")
	languageCoRe = regexp.MustCompile("Select script language|请选择脚本语言")
Z
zhaoke 已提交
44 45 46 47 48 49 50

	productId = 1
	moduleId  = 0
	taskId    = 1
	suiteId   = 1

	productDir = "./product1"
Z
zhaoke 已提交
51 52
)

雨爱无痕 已提交
53
type CoSuite struct {
Z
zhaoke 已提交
54 55 56
	suite.Suite
}

雨爱无痕 已提交
57
func (s *CoSuite) BeforeEach(t provider.T) {
雨爱无痕 已提交
58
	t.ID("1580")
Z
zhaoke 已提交
59
	commonTestHelper.ReplaceLabel(t, "命令行-从禅道同步用例")
Z
zhaoke 已提交
60 61 62 63 64

	if runtime.GOOS == "windows" {
		productDir = ".\\product1"
	}
	productDir = fileUtils.AbsolutePath(productDir)
Z
zhaoke 已提交
65
}
Z
zhaoke 已提交
66

Z
zhaoke 已提交
67
// 当前禅道版本max4.3有bug,当前无法通过测试。bug已修复,未打包
Z
zhaoke 已提交
68
//
Z
zhaoke 已提交
69 70 71 72
func (s *CoSuite) TestCoProduct(t provider.T) {
	t.Title("导出用例,不提供参数")
	t.Require().Equal("Success", testCoProduct())
}
Z
zhaoke 已提交
73

雨爱无痕 已提交
74 75
func (s *CoSuite) TestCoSuite(t provider.T) {
	t.Require().Equal("Success", testCoSuite())
Z
zhaoke 已提交
76
}
Z
zhaoke 已提交
77

雨爱无痕 已提交
78 79 80
func (s *CoSuite) TestCoTask(t provider.T) {
	t.Require().Equal("Success", testCoTask())
}
Z
zhaoke 已提交
81

雨爱无痕 已提交
82
func (s *CoSuite) TestCo(t provider.T) {
Z
zhaoke 已提交
83 84
	// t.Require().Equal("Success", testCo(fmt.Sprintf(commonTestHelper.GetZtfPath()+" co -product %d -language php", productId)))
	// t.Require().Equal("Success", testCo(fmt.Sprintf(commonTestHelper.GetZtfPath()+" co -p %d -m %d -l php", productId, moduleId)))
雨爱无痕 已提交
85 86
	t.Require().Equal("Success", testCo(fmt.Sprintf(commonTestHelper.GetZtfPath()+" co -s %d -l php -i true", suiteId)))
	t.Require().Equal("Success", testCo(fmt.Sprintf(commonTestHelper.GetZtfPath()+" co -t %d -l php", taskId)))
Z
zhaoke 已提交
87 88 89
}

func testCoProduct() string {
Z
zhaoke 已提交
90 91
	os.RemoveAll(productDir)

雨爱无痕 已提交
92
	cmd := commonTestHelper.GetZtfPath() + " co"
Z
zhaoke 已提交
93

Z
zhaoke 已提交
94 95 96 97 98
	child, err := expect.Spawn(cmd, -1)
	if err != nil {
		return err.Error()
	}
	defer child.Close()
Z
zhaoke 已提交
99

雨爱无痕 已提交
100
	if _, err = child.Expect(typeRe, time.Second*5); err != nil {
Z
zhaoke 已提交
101 102
		return fmt.Sprintf("expect %s, actual %s", typeRe, err.Error())
	}
雨爱无痕 已提交
103
	if err = child.Send("1" + constTestHelper.NewLine); err != nil {
Z
zhaoke 已提交
104 105
		return err.Error()
	}
Z
zhaoke 已提交
106

雨爱无痕 已提交
107
	if _, err = child.Expect(productRe, time.Second*5); err != nil {
Z
zhaoke 已提交
108 109
		return fmt.Sprintf("expect %s, actual %s", productRe, err.Error())
	}
雨爱无痕 已提交
110
	if err = child.Send(strconv.Itoa(productId) + constTestHelper.NewLine); err != nil {
Z
zhaoke 已提交
111 112
		return err.Error()
	}
Z
zhaoke 已提交
113

雨爱无痕 已提交
114
	if _, err = child.Expect(moduleRe, time.Second*5); err != nil {
Z
zhaoke 已提交
115 116
		return fmt.Sprintf("expect %s, actual %s", moduleRe, err.Error())
	}
雨爱无痕 已提交
117
	if err = child.Send(strconv.Itoa(moduleId) + constTestHelper.NewLine); err != nil {
Z
zhaoke 已提交
118 119
		return err.Error()
	}
Z
zhaoke 已提交
120

雨爱无痕 已提交
121
	if _, err = child.Expect(separateRe, time.Second*5); err != nil {
Z
zhaoke 已提交
122 123
		return fmt.Sprintf("expect %s, actual %s", separateRe, err.Error())
	}
雨爱无痕 已提交
124
	if err = child.Send("n" + constTestHelper.NewLine); err != nil {
Z
zhaoke 已提交
125 126
		return err.Error()
	}
Z
zhaoke 已提交
127

雨爱无痕 已提交
128
	if _, err = child.Expect(languageCoRe, time.Second*5); err != nil {
Z
zhaoke 已提交
129 130
		return fmt.Sprintf("expect %s, actual %s", languageCoRe, err.Error())
	}
雨爱无痕 已提交
131
	if err = child.Send("5" + constTestHelper.NewLine); err != nil {
Z
zhaoke 已提交
132 133
		return err.Error()
	}
Z
zhaoke 已提交
134

雨爱无痕 已提交
135
	if _, err = child.Expect(storeRe, time.Second*60); err != nil {
Z
zhaoke 已提交
136 137
		return fmt.Sprintf("expect %s, actual %s", storeRe, err.Error())
	}
雨爱无痕 已提交
138
	if err = child.Send(constTestHelper.NewLine); err != nil {
Z
zhaoke 已提交
139 140
		return err.Error()
	}
Z
zhaoke 已提交
141

雨爱无痕 已提交
142
	if _, err = child.Expect(organizeRe, time.Second*5); err != nil {
Z
zhaoke 已提交
143 144
		return fmt.Sprintf("expect %s, actual %s", organizeRe, err.Error())
	}
雨爱无痕 已提交
145
	if err = child.Send(constTestHelper.NewLine); err != nil {
Z
zhaoke 已提交
146 147
		return err.Error()
	}
Z
zhaoke 已提交
148

雨爱无痕 已提交
149 150
	if _, err = child.Expect(successCoRe, 10*time.Second); err != nil {
		return fmt.Sprintf("expect %s, actual %s", successCoRe, err.Error())
Z
zhaoke 已提交
151 152 153 154 155
	}

	return "Success"
}

雨爱无痕 已提交
156
func testCoSuite() string {
Z
zhaoke 已提交
157 158
	os.RemoveAll(productDir)

雨爱无痕 已提交
159
	cmd := commonTestHelper.GetZtfPath() + " co"
Z
zhaoke 已提交
160

Z
zhaoke 已提交
161 162 163 164 165
	child, err := expect.Spawn(cmd, -1)
	if err != nil {
		return err.Error()
	}
	defer child.Close()
Z
zhaoke 已提交
166

雨爱无痕 已提交
167
	if _, err = child.Expect(typeRe, time.Second*5); err != nil {
Z
zhaoke 已提交
168 169
		return fmt.Sprintf("expect %s, actual %s", typeRe, err.Error())
	}
雨爱无痕 已提交
170
	if err = child.Send("2" + constTestHelper.NewLine); err != nil {
Z
zhaoke 已提交
171 172
		return err.Error()
	}
Z
zhaoke 已提交
173

雨爱无痕 已提交
174
	if _, err = child.Expect(suiteRe, time.Second*5); err != nil {
Z
zhaoke 已提交
175 176
		return fmt.Sprintf("expect %s, actual %s", suiteRe, err.Error())
	}
雨爱无痕 已提交
177
	if err = child.Send(strconv.Itoa(suiteId) + constTestHelper.NewLine); err != nil {
Z
zhaoke 已提交
178 179
		return err.Error()
	}
Z
zhaoke 已提交
180

雨爱无痕 已提交
181
	if _, err = child.Expect(separateRe, time.Second*5); err != nil {
Z
zhaoke 已提交
182 183
		return fmt.Sprintf("expect %s, actual %s", separateRe, err.Error())
	}
雨爱无痕 已提交
184
	if err = child.Send("n" + constTestHelper.NewLine); err != nil {
Z
zhaoke 已提交
185 186
		return err.Error()
	}
Z
zhaoke 已提交
187

雨爱无痕 已提交
188
	if _, err = child.Expect(languageCoRe, time.Second*5); err != nil {
Z
zhaoke 已提交
189 190
		return fmt.Sprintf("expect %s, actual %s", languageCoRe, err.Error())
	}
雨爱无痕 已提交
191
	if err = child.Send("5" + constTestHelper.NewLine); err != nil {
Z
zhaoke 已提交
192 193
		return err.Error()
	}
Z
zhaoke 已提交
194

雨爱无痕 已提交
195
	if _, err = child.Expect(storeRe, time.Second*60); err != nil {
Z
zhaoke 已提交
196 197
		return fmt.Sprintf("expect %s, actual %s", storeRe, err.Error())
	}
雨爱无痕 已提交
198
	if err = child.Send(constTestHelper.NewLine); err != nil {
Z
zhaoke 已提交
199 200
		return err.Error()
	}
Z
zhaoke 已提交
201

雨爱无痕 已提交
202
	if _, err = child.Expect(organizeRe, time.Second*5); err != nil {
Z
zhaoke 已提交
203 204
		return fmt.Sprintf("expect %s, actual %s", organizeRe, err.Error())
	}
雨爱无痕 已提交
205
	if err = child.Send(constTestHelper.NewLine); err != nil {
Z
zhaoke 已提交
206 207
		return err.Error()
	}
Z
zhaoke 已提交
208

雨爱无痕 已提交
209 210
	if _, err = child.Expect(successCoRe, 10*time.Second); err != nil {
		return fmt.Sprintf("expect %s, actual %s", successCoRe, err.Error())
Z
zhaoke 已提交
211 212
	}

Z
zhaoke 已提交
213 214 215 216
	if !fileUtils.FileExist(path.Join(productDir, "1.pl")) {
		return "expect 1.pl exist, actual not exist"
	}

Z
zhaoke 已提交
217 218 219 220
	return "Success"
}

func testCoTask() string {
Z
zhaoke 已提交
221 222
	os.RemoveAll(productDir)

雨爱无痕 已提交
223
	cmd := commonTestHelper.GetZtfPath() + " co"
Z
zhaoke 已提交
224

Z
zhaoke 已提交
225 226 227 228 229
	child, err := expect.Spawn(cmd, -1)
	if err != nil {
		return err.Error()
	}
	defer child.Close()
Z
zhaoke 已提交
230

雨爱无痕 已提交
231
	if _, err = child.Expect(typeRe, time.Second*5); err != nil {
Z
zhaoke 已提交
232 233
		return fmt.Sprintf("expect %s, actual %s", typeRe, err.Error())
	}
雨爱无痕 已提交
234
	if err = child.Send("3" + constTestHelper.NewLine); err != nil {
Z
zhaoke 已提交
235 236
		return err.Error()
	}
Z
zhaoke 已提交
237

雨爱无痕 已提交
238
	if _, err = child.Expect(taskRe, time.Second*5); err != nil {
Z
zhaoke 已提交
239 240
		return fmt.Sprintf("expect %s, actual %s", taskRe, err.Error())
	}
雨爱无痕 已提交
241
	if err = child.Send(strconv.Itoa(taskId) + constTestHelper.NewLine); err != nil {
Z
zhaoke 已提交
242 243
		return err.Error()
	}
Z
zhaoke 已提交
244

雨爱无痕 已提交
245
	if _, err = child.Expect(separateRe, time.Second*5); err != nil {
Z
zhaoke 已提交
246 247
		return fmt.Sprintf("expect %s, actual %s", separateRe, err.Error())
	}
雨爱无痕 已提交
248
	if err = child.Send("n" + constTestHelper.NewLine); err != nil {
Z
zhaoke 已提交
249 250
		return err.Error()
	}
Z
zhaoke 已提交
251

雨爱无痕 已提交
252
	if _, err = child.Expect(languageCoRe, time.Second*5); err != nil {
Z
zhaoke 已提交
253 254
		return fmt.Sprintf("expect %s, actual %s", languageCoRe, err.Error())
	}
雨爱无痕 已提交
255
	if err = child.Send("5" + constTestHelper.NewLine); err != nil {
Z
zhaoke 已提交
256 257
		return err.Error()
	}
Z
zhaoke 已提交
258

雨爱无痕 已提交
259
	if _, err = child.Expect(storeRe, time.Second*60*5); err != nil {
Z
zhaoke 已提交
260 261
		return fmt.Sprintf("expect %s, actual %s", storeRe, err.Error())
	}
雨爱无痕 已提交
262
	if err = child.Send(constTestHelper.NewLine); err != nil {
Z
zhaoke 已提交
263 264
		return err.Error()
	}
Z
zhaoke 已提交
265

雨爱无痕 已提交
266
	if _, err = child.Expect(organizeRe, time.Second*5); err != nil {
Z
zhaoke 已提交
267 268
		return fmt.Sprintf("expect %s, actual %s", organizeRe, err.Error())
	}
雨爱无痕 已提交
269
	if err = child.Send(constTestHelper.NewLine); err != nil {
Z
zhaoke 已提交
270 271
		return err.Error()
	}
Z
zhaoke 已提交
272

雨爱无痕 已提交
273 274
	if _, err = child.Expect(successCoRe, 10*time.Second); err != nil {
		return fmt.Sprintf("expect %s, actual %s", successCoRe, err.Error())
Z
zhaoke 已提交
275 276
	}

Z
zhaoke 已提交
277 278 279 280
	if !fileUtils.FileExist(path.Join(productDir, "2.pl")) {
		return "expect 2.pl exist, actual not exist"
	}

Z
zhaoke 已提交
281 282 283 284
	return "Success"
}

func testCo(cmd string) string {
Z
zhaoke 已提交
285 286
	os.RemoveAll(productDir)

Z
zhaoke 已提交
287
	child, err := expect.Spawn(cmd, -1)
Z
zhaoke 已提交
288

Z
zhaoke 已提交
289 290 291 292 293
	if err != nil {
		return err.Error()
	}
	defer child.Close()

雨爱无痕 已提交
294
	if _, err = child.Expect(storeRe, time.Second*60); err != nil {
Z
zhaoke 已提交
295 296
		return fmt.Sprintf("expect %s, actual %s", storeRe, err.Error())
	}
雨爱无痕 已提交
297
	if err = child.Send(constTestHelper.NewLine); err != nil {
Z
zhaoke 已提交
298 299
		return err.Error()
	}
Z
zhaoke 已提交
300

雨爱无痕 已提交
301
	if _, err = child.Expect(organizeRe, time.Second*5); err != nil {
Z
zhaoke 已提交
302 303
		return fmt.Sprintf("expect %s, actual %s", organizeRe, err.Error())
	}
雨爱无痕 已提交
304
	if err = child.Send(constTestHelper.NewLine); err != nil {
Z
zhaoke 已提交
305 306
		return err.Error()
	}
Z
zhaoke 已提交
307

雨爱无痕 已提交
308 309
	if _, err = child.Expect(successCoRe, 10*time.Second); err != nil {
		return fmt.Sprintf("expect %s, actual %s", successCoRe, err.Error())
Z
zhaoke 已提交
310 311
	}

Z
zhaoke 已提交
312 313 314 315 316 317 318 319 320 321
	if strings.Contains(cmd, "-s") {
		if !fileUtils.FileExist(path.Join(productDir, "1.exp")) {
			return "expect 1.exp exist, actual not exist"
		}
	} else {
		if !fileUtils.FileExist(path.Join(productDir, "2.php")) {
			return "expect 2.php exist, actual not exist"
		}
	}

Z
zhaoke 已提交
322 323 324
	return "Success"
}

Save  
雨爱无痕 已提交
325
func TestCliCo(t *testing.T) {
雨爱无痕 已提交
326
	suite.RunSuite(t, new(CoSuite))
Z
zhaoke 已提交
327
}