8分鐘閱讀
> https://wesovilabs-tools.github.io/orion
Orion誕生是為了幫助我們編寫和自動化驗收測試。它提供了受Gherkin啟發並基於HCL的DSL。
Orion的目標是為沒有技術技能的人們提供編寫驗收測試的工具。
在本文中,我們將透過一些實際場景來學習如何處理Orion。
展示一:數學運算我們將編寫驗收測試以驗證操作加法和減法是否正確。
初始方法我們將編寫一些方案來驗證以下內容:
10 + 5 = 1510 -5 = 5# feature-math-operations.hcldescription = <<EOF This feature is used to demonstrate that both add and subs operations work as expected.EOFscenario "operation add" { given "the variables x and y" { set x { value = 10 } set y{ value = 5 } } when "values are added" { set result { value = x + y } print { msg = "${x} + ${y} is ${result}" } } then "the result of the operation is the expected" { assert { assertion = result==15 } }}scenario "operation substract" { given "variables x and y" { set x { value = 10 } set y{ value = 5 } } when "subtract y to x" { set result { value = x - y } print { msg = "${x} - ${y} is ${result}" } } then "the result of the operation is the expected" { assert { assertion = result==5 } }}
讓執行程式執行orion run --input feature-math-operations.hcl
Hooks掛鉤允許我們編寫一組可重用的動作,可以在場景之前或之後執行。訪問Hooks文件
# feature-math-operations.hcldescription = <<EOF This feature is used to demonstrate that both add and subs operations work as expected.EOFafter each { print { msg = "the output of this operation is ${result}" }}scenario "operation add" { given "the variables x and y" { set x { value = 10 } set y{ value = 5 } } when "values are added" { set result { value = x + y } } then "the result of the operation is the expected" { assert { assertion = result==15 } }}scenario "operation substract" { given "variables x and y" { set x { value = 10 } set y{ value = 5 } } when "subtract y to x" { set result { value = x - y } } then "the result of the operation is the expected" { assert { assertion = result==5 } }}
讓執行程式執行orion run --input feature-math-operations.hcl
輸入變數更好的方法是使用不同的資料執行相同的方案。我們可以使用塊輸入來做到這一點。訪問輸入引數文件
# feature-math-operations.hcldescription = <<EOF This feature is used to demonstrate that both add and subs operations work as expected.EOFinput { arg x { default = 10 } arg y { default = 5 } arg sumResult { default = 15 } arg subResult { default = 5 }}after each { print { msg = "the output of this operation is ${result}" }}scenario "operation add" { when "values are added" { set result { value = x + y } } then "the result of the operation is the expected" { assert { assertion = result==sumResult } }}scenario "operation substract" { when "subtract y to x" { set result { value = x - y } } then "the result of the operation is the expected" { assert { assertion = result==subResult } }}
由於我們為所有args定義了預設值,因此我們可以像往常一樣執行功能:orion run –input feature-math-operations.hcl
另一方面,我們可以為變數設定值,如下所示
# variables-math-operations.hclx = 54y = 46sumResult = 100subResult = 8
讓執行程式執行orion run --input feature-math-operations.hcl --vars vars-math-operations.hcl
跳過方案屬性忽略允許我們跳過方案的執行。例如,我們可以確定當x> 10時將忽略第二種情況。
# feature-math-operations.hcldescription = <<EOF This feature is used to demonstrate that both add and subs operations work as expected.EOFinput { arg x { default = 10 } arg y { default = 5 } arg sumResult { default = 15 } arg subResult { default = 5 }}after each { print { msg = "the output of this operation is ${result}" }}scenario "operation add" { when "values are added" { set result { value = x + y } } then "the result of the operation is the expected" { assert { assertion = result==sumResult } }}scenario "operation substract" { ignore = x > 10 when "subtract y to x" { set result { value = x - y } } then "the result of the operation is the expected" { assert { assertion = result==subResult } }}
如果我們不傳遞變數檔案,則兩種情況都將執行,因為x的預設值為10。另一方面,如果我們傳遞變數檔案,則因為x為54,將不執行第二種情況。
包括我們可以將檔案的內容分為幾個檔案,然後將它們重用於不同的功能。訪問包括文件。為了付諸實踐,我們將場景移動到單獨的檔案中。
# scenario-sum.hclscenario "operation add" { when "values are added" { set result { value = x + y } } then "the result of the operation is the expected" { assert { assertion = result==sumResult } }}# scenario-sub.hclscenario "operation substract" { ignore = x > 10 when "subtract y to x" { set result { value = x - y } } then "the result of the operation is the expected" { assert { assertion = result==subResult } }}
然後,我們只利用屬性包括
# feature-math-operations.hcldescription = <<EOF This feature is used to demonstrate that both add and subs operations work as expected.EOFinput { arg x { default = 10 } arg y { default = 5 } arg sumResult { default = 15 } arg subResult { default = 5 }}after each { print { msg = "the output of this operation is ${result}" }}includes = [ "scenario-sum.hcl", "scenario-sub.hcl"]
讓執行程式執行orion run --input feature-math-operations.hcl
有條件的行動屬性when允許我們定義動作是否執行。所有操作均具有此可選屬性。我們在第二種情況下進行以下更改:當x> y時為x-y,否則為y-x
# scenario-sub.hclscenario "operation substract" { when "subtract y to x" { set result { value = x - y when = x > y } set result { value = y - x when = x <= y } } then "the result of the operation is the expected" { assert { assertion = result==subResult } }}
如果我們在檔案vars-math-operations.hcl中交換x和y的值
# variables-math-operations.hclx = 46 y = 54sumResult = 100
我們的方案將成功執行
讓執行程式執行orion run --input feature-math-operations.hcl --vars vars-math-operations.hcl
多種情況如果您有使用Cucumber的經驗,這將記住場景概述。我們只需要在場景的屬性示例中定義一組輸入資料即可。例如,我們將提供第二種情況的示例。
# scenario-sub.hclscenario "operation substract" { examples = [ { x = 20, y = 10, subResult= 10}, { x = 10, y = 20, subResult= 10}, { x = 5, y = 5, subResult= 0}, ] when "subtract y to x" { set result { value = x - y when = x > y } set result { value = y - x when = x <= y } } then "the result of the operation is the expected" { assert { assertion = result==subResult } }}
如果我們執行Orion,則上述情況將執行3次。
讓執行程式執行orion run --input feature-math-operations.hcl
特殊動作:塊讓我們實現一個新的場景,以演示乘法的工作原理。最初,我們可以去做這樣的事情
# scenario-mult.hclscenario "operation multiplication" { examples = [ { x = 20, y = 10, multResult= 10}, { x = -1, y = -2, multResult= 2}, { x = 5, y = 5, multResult= 25}, { x = 5, y = 0, multResult= 0}, ] when "multiply y by x" { set result { value = x * y } } then "the result of the operation is the expected" { assert { assertion = result==multResult } }}
但我想向您展示特殊操作塊如何工作。該操作用於對一組操作進行分組。另外,我們將利用允許從0迭代到count-1的屬性計數。
# scenario-mult.hclscenario "operation multiplication" { examples = [ { x = 20, y = 10, multResult= 200}, { x = -1, y = -2, multResult= 2}, { x = 5, y = 5, multResult= 25}, { x = 5, y = 0, multResult= 0}, ] given "initialie result" { set result { value = 0 } } when "multiply y by x" { block { set result { value = result + x } print { msg = "${x} * ${_.index+1} is ${result}" } count = y when = x>0 && y>0 } set result { value = x * y when = x<0 || y<0 } } then "the result of the operation is the expected" { assert { assertion = result==multResult } }}
當然,我們需要將此檔案新增到include塊中。
# feature-math-operations.hcldescription = <<EOF This feature is used to demonstrate that both add and subs operations work as expected.EOFinput { arg x { default = 10 } arg y { default = 5 } arg sumResult { default = 15 } arg subResult { default = 5 }}after each { print { msg = "the output of this operation is ${result}" }}includes = [ "scenario-sum.hcl", "scenario-sub.hcl", "scenario-mult.hcl"]
讓執行程式執行orion run --input feature-math-operations.hcl
除了時間和計數外,還有其他特殊屬性。在此處檢視完整列表。
Orion仍處於測試階段,因此,您的建議和反饋將非常感謝!