mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 23:51:25 +08:00
feat: set testcase and request timeout in seconds
This commit is contained in:
+4
-1
@@ -37,6 +37,7 @@ var (
|
|||||||
proxyUrl string
|
proxyUrl string
|
||||||
saveTests bool
|
saveTests bool
|
||||||
genHTMLReport bool
|
genHTMLReport bool
|
||||||
|
caseTimeout float32
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -48,12 +49,14 @@ func init() {
|
|||||||
runCmd.Flags().StringVarP(&proxyUrl, "proxy-url", "p", "", "set proxy url")
|
runCmd.Flags().StringVarP(&proxyUrl, "proxy-url", "p", "", "set proxy url")
|
||||||
runCmd.Flags().BoolVarP(&saveTests, "save-tests", "s", false, "save tests summary")
|
runCmd.Flags().BoolVarP(&saveTests, "save-tests", "s", false, "save tests summary")
|
||||||
runCmd.Flags().BoolVarP(&genHTMLReport, "gen-html-report", "g", false, "generate html report")
|
runCmd.Flags().BoolVarP(&genHTMLReport, "gen-html-report", "g", false, "generate html report")
|
||||||
|
runCmd.Flags().Float32Var(&caseTimeout, "case-timeout", 3600, "set testcase timeout (seconds)")
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeHRPRunner() *hrp.HRPRunner {
|
func makeHRPRunner() *hrp.HRPRunner {
|
||||||
runner := hrp.NewRunner(nil).
|
runner := hrp.NewRunner(nil).
|
||||||
SetFailfast(!continueOnFailure).
|
SetFailfast(!continueOnFailure).
|
||||||
SetSaveTests(saveTests)
|
SetSaveTests(saveTests).
|
||||||
|
SetCaseTimeout(caseTimeout)
|
||||||
if genHTMLReport {
|
if genHTMLReport {
|
||||||
runner.GenHTMLReport()
|
runner.GenHTMLReport()
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-5
@@ -2,7 +2,6 @@ package hrp
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
||||||
"github.com/httprunner/httprunner/v4/hrp/pkg/uixt"
|
"github.com/httprunner/httprunner/v4/hrp/pkg/uixt"
|
||||||
@@ -32,7 +31,8 @@ type TConfig struct {
|
|||||||
WebSocketSetting *WebSocketConfig `json:"websocket,omitempty" yaml:"websocket,omitempty"`
|
WebSocketSetting *WebSocketConfig `json:"websocket,omitempty" yaml:"websocket,omitempty"`
|
||||||
IOS []*uixt.IOSDevice `json:"ios,omitempty" yaml:"ios,omitempty"`
|
IOS []*uixt.IOSDevice `json:"ios,omitempty" yaml:"ios,omitempty"`
|
||||||
Android []*uixt.AndroidDevice `json:"android,omitempty" yaml:"android,omitempty"`
|
Android []*uixt.AndroidDevice `json:"android,omitempty" yaml:"android,omitempty"`
|
||||||
Timeout float64 `json:"timeout,omitempty" yaml:"timeout,omitempty"` // global timeout in seconds
|
RequestTimeout float32 `json:"request_timeout,omitempty" yaml:"request_timeout,omitempty"` // request timeout in seconds
|
||||||
|
CaseTimeout float32 `json:"case_timeout,omitempty" yaml:"case_timeout,omitempty"` // testcase timeout in seconds
|
||||||
Export []string `json:"export,omitempty" yaml:"export,omitempty"`
|
Export []string `json:"export,omitempty" yaml:"export,omitempty"`
|
||||||
Weight int `json:"weight,omitempty" yaml:"weight,omitempty"`
|
Weight int `json:"weight,omitempty" yaml:"weight,omitempty"`
|
||||||
Path string `json:"path,omitempty" yaml:"path,omitempty"` // testcase file path
|
Path string `json:"path,omitempty" yaml:"path,omitempty"` // testcase file path
|
||||||
@@ -75,9 +75,15 @@ func (c *TConfig) SetThinkTime(strategy thinkTimeStrategy, cfg interface{}, limi
|
|||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetTimeout sets testcase timeout in seconds.
|
// SetRequestTimeout sets request timeout in seconds.
|
||||||
func (c *TConfig) SetTimeout(timeout time.Duration) *TConfig {
|
func (c *TConfig) SetRequestTimeout(seconds float32) *TConfig {
|
||||||
c.Timeout = timeout.Seconds()
|
c.RequestTimeout = seconds
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCaseTimeout sets testcase timeout in seconds.
|
||||||
|
func (c *TConfig) SetCaseTimeout(seconds float32) *TConfig {
|
||||||
|
c.CaseTimeout = seconds
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+25
-7
@@ -21,6 +21,7 @@ import (
|
|||||||
"golang.org/x/net/http2"
|
"golang.org/x/net/http2"
|
||||||
|
|
||||||
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
||||||
|
"github.com/httprunner/httprunner/v4/hrp/internal/code"
|
||||||
"github.com/httprunner/httprunner/v4/hrp/internal/sdk"
|
"github.com/httprunner/httprunner/v4/hrp/internal/sdk"
|
||||||
"github.com/httprunner/httprunner/v4/hrp/internal/version"
|
"github.com/httprunner/httprunner/v4/hrp/internal/version"
|
||||||
"github.com/httprunner/httprunner/v4/hrp/pkg/uixt"
|
"github.com/httprunner/httprunner/v4/hrp/pkg/uixt"
|
||||||
@@ -60,6 +61,7 @@ func NewRunner(t *testing.T) *HRPRunner {
|
|||||||
wsDialer: &websocket.Dialer{
|
wsDialer: &websocket.Dialer{
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
},
|
},
|
||||||
|
caseTimeoutTimer: time.NewTimer(time.Hour * 2), // default case timeout to 2 hour
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,6 +78,7 @@ type HRPRunner struct {
|
|||||||
http2Client *http.Client
|
http2Client *http.Client
|
||||||
wsDialer *websocket.Dialer
|
wsDialer *websocket.Dialer
|
||||||
uiClients map[string]*uixt.DriverExt // UI automation clients for iOS and Android, key is udid/serial
|
uiClients map[string]*uixt.DriverExt // UI automation clients for iOS and Android, key is udid/serial
|
||||||
|
caseTimeoutTimer *time.Timer // case timeout timer
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetClientTransport configures transport of http client for high concurrency load testing
|
// SetClientTransport configures transport of http client for high concurrency load testing
|
||||||
@@ -152,10 +155,17 @@ func (r *HRPRunner) SetProxyUrl(proxyUrl string) *HRPRunner {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetTimeout configures global timeout in seconds.
|
// SetRequestTimeout configures global request timeout in seconds.
|
||||||
func (r *HRPRunner) SetTimeout(timeout time.Duration) *HRPRunner {
|
func (r *HRPRunner) SetRequestTimeout(seconds float32) *HRPRunner {
|
||||||
log.Info().Float64("timeout(seconds)", timeout.Seconds()).Msg("[init] SetTimeout")
|
log.Info().Float32("timeout_seconds", seconds).Msg("[init] SetRequestTimeout")
|
||||||
r.httpClient.Timeout = timeout
|
r.httpClient.Timeout = time.Duration(seconds*1000) * time.Millisecond
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCaseTimeout configures global testcase timeout in seconds.
|
||||||
|
func (r *HRPRunner) SetCaseTimeout(seconds float32) *HRPRunner {
|
||||||
|
log.Info().Float32("timeout_seconds", seconds).Msg("[init] SetCaseTimeout")
|
||||||
|
r.caseTimeoutTimer = time.NewTimer(time.Duration(seconds*1000) * time.Millisecond)
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -291,10 +301,13 @@ func (r *HRPRunner) NewCaseRunner(testcase *TestCase) (*CaseRunner, error) {
|
|||||||
return nil, errors.Wrap(err, "parse testcase config failed")
|
return nil, errors.Wrap(err, "parse testcase config failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set request timeout in seconds
|
||||||
|
if testcase.Config.RequestTimeout != 0 {
|
||||||
|
r.SetRequestTimeout(testcase.Config.RequestTimeout)
|
||||||
|
}
|
||||||
// set testcase timeout in seconds
|
// set testcase timeout in seconds
|
||||||
if testcase.Config.Timeout != 0 {
|
if testcase.Config.CaseTimeout != 0 {
|
||||||
timeout := time.Duration(testcase.Config.Timeout*1000) * time.Millisecond
|
r.SetCaseTimeout(testcase.Config.CaseTimeout)
|
||||||
r.SetTimeout(timeout)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// load plugin info to testcase config
|
// load plugin info to testcase config
|
||||||
@@ -505,6 +518,10 @@ func (r *SessionRunner) Start(givenVars map[string]interface{}) error {
|
|||||||
|
|
||||||
// run step in sequential order
|
// run step in sequential order
|
||||||
for _, step := range r.caseRunner.testCase.TestSteps {
|
for _, step := range r.caseRunner.testCase.TestSteps {
|
||||||
|
select {
|
||||||
|
case <-r.caseRunner.hrpRunner.caseTimeoutTimer.C:
|
||||||
|
return errors.Wrap(code.TimeoutError, "session runner timeout")
|
||||||
|
default:
|
||||||
// TODO: parse step struct
|
// TODO: parse step struct
|
||||||
// parse step name
|
// parse step name
|
||||||
parsedName, err := r.caseRunner.parser.ParseString(step.Name(), r.sessionVariables)
|
parsedName, err := r.caseRunner.parser.ParseString(step.Name(), r.sessionVariables)
|
||||||
@@ -567,6 +584,7 @@ func (r *SessionRunner) Start(givenVars map[string]interface{}) error {
|
|||||||
return errors.Wrap(err, "abort running due to failfast setting")
|
return errors.Wrap(err, "abort running due to failfast setting")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
log.Info().Str("testcase", config.Name).Msg("run testcase end")
|
log.Info().Str("testcase", config.Name).Msg("run testcase end")
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -164,7 +164,7 @@ func TestRunCaseWithTimeout(t *testing.T) {
|
|||||||
// global timeout
|
// global timeout
|
||||||
testcase1 := &TestCase{
|
testcase1 := &TestCase{
|
||||||
Config: NewConfig("TestCase1").
|
Config: NewConfig("TestCase1").
|
||||||
SetTimeout(10 * time.Second). // set global timeout to 10s
|
SetRequestTimeout(10). // set global timeout to 10s
|
||||||
SetBaseURL("https://httpbin.org"),
|
SetBaseURL("https://httpbin.org"),
|
||||||
TestSteps: []IStep{
|
TestSteps: []IStep{
|
||||||
NewStep("step1").
|
NewStep("step1").
|
||||||
@@ -180,7 +180,7 @@ func TestRunCaseWithTimeout(t *testing.T) {
|
|||||||
|
|
||||||
testcase2 := &TestCase{
|
testcase2 := &TestCase{
|
||||||
Config: NewConfig("TestCase2").
|
Config: NewConfig("TestCase2").
|
||||||
SetTimeout(10 * time.Second). // set global timeout to 10s
|
SetRequestTimeout(10). // set global timeout to 10s
|
||||||
SetBaseURL("https://httpbin.org"),
|
SetBaseURL("https://httpbin.org"),
|
||||||
TestSteps: []IStep{
|
TestSteps: []IStep{
|
||||||
NewStep("step1").
|
NewStep("step1").
|
||||||
@@ -197,7 +197,7 @@ func TestRunCaseWithTimeout(t *testing.T) {
|
|||||||
// step timeout
|
// step timeout
|
||||||
testcase3 := &TestCase{
|
testcase3 := &TestCase{
|
||||||
Config: NewConfig("TestCase3").
|
Config: NewConfig("TestCase3").
|
||||||
SetTimeout(10 * time.Second).
|
SetRequestTimeout(10).
|
||||||
SetBaseURL("https://httpbin.org"),
|
SetBaseURL("https://httpbin.org"),
|
||||||
TestSteps: []IStep{
|
TestSteps: []IStep{
|
||||||
NewStep("step2").
|
NewStep("step2").
|
||||||
|
|||||||
Reference in New Issue
Block a user