Skip to content

Commit

Permalink
+ [feat]: refactor data types and structures for test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ysicing committed Jul 19, 2024
1 parent fba57e5 commit 4d05e7b
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 42 deletions.
58 changes: 58 additions & 0 deletions example/testcases/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// Copyright 2024, zentao
//
// 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 main

import (
"log"

"github.com/davecgh/go-spew/spew"
"github.com/easysoft/go-zentao/v20/zentao"
)

func main() {
zt, err := zentao.NewBasicAuthClient(
"ysicing",
"Zt@1234",
zentao.WithBaseURL("http://192.168.94.17:8080"),
zentao.WithDevMode(),
zentao.WithDumpAll(),
zentao.WithoutProxy(),
)
if err != nil {
log.Fatal(err)
}
p1tc, _, err := zt.TestCases.ListByProducts(1)
if err != nil {
log.Fatal(err)
}
spew.Dump(p1tc)
p2tc, _, err := zt.TestCases.ListByProducts(1, map[string]string{"module": "1"})
if err != nil {
log.Fatal(err)
}
spew.Dump(p2tc)
ts, _, err := zt.TestCases.GetResultByID("4")
if err != nil {
log.Fatal(err)
}
spew.Dump(ts)
ts1, _, err := zt.TestCases.GetResultByID("case_4")
if err != nil {
log.Fatal(err)
}
spew.Dump(ts1)
}
146 changes: 104 additions & 42 deletions zentao/testcases.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package zentao

import (
"fmt"
"strings"
"time"

"github.com/imroc/req/v3"
Expand All @@ -28,7 +29,7 @@ type TestCasesService struct {
}

type TestCasesMeta struct {
ID int `json:"id"`
ID any `json:"id"` // 老版本int, 新版本string
Project int `json:"project"`
Product int `json:"product"`
Execution int `json:"execution"`
Expand All @@ -53,35 +54,35 @@ type TestCasesMeta struct {

type TestcasesListBody struct {
TestCasesMeta
Lib int `json:"lib"`
Path int `json:"path"`
Precondition string `json:"precondition"`
Auto string `json:"auto"`
Frame string `json:"frame"`
Stage string `json:"stage"`
Howrun string `json:"howRun"`
Scriptedby string `json:"scriptedBy"`
Scripteddate interface{} `json:"scriptedDate"`
Scriptstatus string `json:"scriptStatus"`
Scriptlocation string `json:"scriptLocation"`
Frequency string `json:"frequency"`
Order int `json:"order"`
Reviewedby interface{} `json:"reviewedBy"`
Revieweddate interface{} `json:"reviewedDate"`
Version int `json:"version"`
Linkcase string `json:"linkCase"`
Frombug int `json:"fromBug"`
Fromcaseid int `json:"fromCaseID"`
Fromcaseversion int `json:"fromCaseVersion"`
Lastrunner string `json:"lastRunner"`
Lastrundate interface{} `json:"lastRunDate"`
Lastrunresult string `json:"lastRunResult"`
Needconfirm bool `json:"needconfirm"`
Bugs int `json:"bugs"`
Results int `json:"results"`
Casefails int `json:"caseFails"`
Stepnumber int `json:"stepNumber"`
Statusname string `json:"statusName"`
Lib int `json:"lib"`
Path int `json:"path"`
Precondition string `json:"precondition"`
Auto string `json:"auto"`
Frame string `json:"frame"`
Stage string `json:"stage"`
Howrun string `json:"howRun"`
Scriptedby string `json:"scriptedBy"`
Scripteddate any `json:"scriptedDate"`
Scriptstatus string `json:"scriptStatus"`
Scriptlocation string `json:"scriptLocation"`
Frequency string `json:"frequency"`
Order int `json:"order"`
Reviewedby any `json:"reviewedBy"`
Revieweddate any `json:"reviewedDate"`
Version int `json:"version"`
Linkcase string `json:"linkCase"`
Frombug int `json:"fromBug"`
Fromcaseid int `json:"fromCaseID"`
Fromcaseversion int `json:"fromCaseVersion"`
Lastrunner string `json:"lastRunner"`
Lastrundate any `json:"lastRunDate"`
Lastrunresult string `json:"lastRunResult"`
Needconfirm bool `json:"needconfirm"`
Bugs int `json:"bugs"`
Results int `json:"results"`
Casefails int `json:"caseFails"`
Stepnumber int `json:"stepNumber"`
Statusname string `json:"statusName"`
}

type ListProductsTestCasesMsg struct {
Expand Down Expand Up @@ -114,20 +115,20 @@ type TestCasesCreateMsg struct {
Stage string `json:"stage"`
Howrun string `json:"howRun"`
Scriptedby string `json:"scriptedBy"`
Scripteddate interface{} `json:"scriptedDate"`
Scripteddate any `json:"scriptedDate"`
Scriptstatus string `json:"scriptStatus"`
Scriptlocation string `json:"scriptLocation"`
Frequency string `json:"frequency"`
Order int `json:"order"`
Reviewedby string `json:"reviewedBy"`
Revieweddate interface{} `json:"reviewedDate"`
Revieweddate any `json:"reviewedDate"`
Version int `json:"version"`
Linkcase string `json:"linkCase"`
Frombug int `json:"fromBug"`
Fromcaseid int `json:"fromCaseID"`
Fromcaseversion int `json:"fromCaseVersion"`
Lastrunner string `json:"lastRunner"`
Lastrundate interface{} `json:"lastRunDate"`
Lastrundate any `json:"lastRunDate"`
Lastrunresult string `json:"lastRunResult"`
Tobugs []interface{} `json:"toBugs"`
Steps []TestCasesStep `json:"steps"`
Expand Down Expand Up @@ -166,11 +167,11 @@ type TestCasesGetMsg struct {
Openedbuild string `json:"openedBuild"`
Assignedto UserMeta `json:"assignedTo"`
Assigneddate time.Time `json:"assignedDate"`
Deadline interface{} `json:"deadline"`
Deadline any `json:"deadline"`
Resolvedby UserMeta `json:"resolvedBy"`
Resolution string `json:"resolution"`
Resolvedbuild string `json:"resolvedBuild"`
Resolveddate interface{} `json:"resolvedDate"`
Resolveddate any `json:"resolvedDate"`
Closedby UserMeta `json:"closedBy"`
Closeddate time.Time `json:"closedDate"`
Duplicatebug int `json:"duplicateBug"`
Expand All @@ -188,17 +189,62 @@ type TestCasesGetMsg struct {
Executionname string `json:"executionName"`
Storystatus string `json:"storyStatus"`
Lateststoryversion int `json:"latestStoryVersion"`
Taskname interface{} `json:"taskName"`
Planname interface{} `json:"planName"`
Taskname any `json:"taskName"`
Planname any `json:"planName"`
Projectname string `json:"projectName"`
Tocases []interface{} `json:"toCases"`
Files []interface{} `json:"files"`
Tocases []any `json:"toCases"`
Files []any `json:"files"`
}

func (s *TestCasesService) ListByProducts(id int) (*ListProductsTestCasesMsg, *req.Response, error) {
type TestCasesResult struct {
ID int `json:"id"`
Run int `json:"run"`
Case int `json:"case"`
Version int `json:"version"`
Job int `json:"job"`
Compile int `json:"compile"`
CaseResult string `json:"caseResult"` // 用例结果 pass
StepResults []TestCasesStepResult `json:"stepResults"`
ZTFResult string `json:"ZTFResult"`
Node int `json:"node"`
Lastrunner string `json:"lastRunner"`
Date string `json:"date"`
Duration int `json:"duration"`
Xml any `json:"xml"`
Deploy int `json:"deploy"`
Build any `json:"build"`
Task int `json:"task"`
NodeName string `json:"nodeName"`
Files []any `json:"files"`
}

type TestCasesStepResult struct {
ID int `json:"id"`
Parent int `json:"parent"`
Case int `json:"case"`
Version int `json:"version"`
Type string `json:"type"` // 步骤类型 step
Desc string `json:"desc"`
Expect string `json:"expect"`
Result string `json:"result"`
Name string `json:"name"`
Grade int `json:"grade"`
Real string `json:"real"`
Files []any `json:"files"`
}

type TestCasesResultResp struct {
Results []TestCasesResult `json:"results"`
}

func (s *TestCasesService) ListByProducts(id int, parms ...any) (*ListProductsTestCasesMsg, *req.Response, error) {
var et ListProductsTestCasesMsg
resp, err := s.client.client.R().
SetHeader("Token", s.client.token).
req := s.client.client.R().
SetHeader("Token", s.client.token)
if len(parms) > 0 {
req = req.SetQueryParams(parms[0].(map[string]string))
}
resp, err := req.
SetSuccessResult(&et).
Get(s.client.RequestURL(fmt.Sprintf("/products/%d/testcases", id)))
return &et, resp, err
Expand Down Expand Up @@ -245,3 +291,19 @@ func (s *TestCasesService) GetByID(id int) (*TestCasesCreateMsg, *req.Response,
Get(s.client.RequestURL(fmt.Sprintf("/testcases/%d", id)))
return &u, resp, err
}

// GetByID 获取用例详情
func (s *TestCasesService) GetResultByID(id any) (*TestCasesResultResp, *req.Response, error) {
var u TestCasesResultResp
// 判断id的类型,如果是string,就是用例id,如果是int,就是结果id
if _, ok := id.(string); ok {
if strings.HasPrefix(id.(string), "case_") {
id = strings.TrimPrefix(id.(string), "case_")
}
}
resp, err := s.client.client.R().
SetHeader("Token", s.client.token).
SetSuccessResult(&u).
Get(s.client.RequestURL(fmt.Sprintf("/testcases/%v/results", id)))
return &u, resp, err
}

0 comments on commit 4d05e7b

Please sign in to comment.