在golang中,要从Gherkin datatable输入布尔值,可以通过以下步骤实现:
godog
库来处理Gherkin特性文件和场景。确保你已经安装了godog
库。Feature: Golang Gherkin Datatable
Scenario: Example scenario
Given I have a datatable with boolean values
| is_enabled |
| true |
| false |
| true |
.feature
文件并命名为datatable.feature
,然后编写以下代码:package main
import (
"fmt"
"github.com/DATA-DOG/godog"
"github.com/DATA-DOG/godog/gherkin"
"strconv"
)
type datatableFeature struct {
isEnabled []bool
}
func (f *datatableFeature) iHaveADataTableWithBooleanValues(table *gherkin.DataTable) error {
for _, row := range table.Rows {
isEnabled, err := strconv.ParseBool(row.Cells[0].Value)
if err != nil {
return err
}
f.isEnabled = append(f.isEnabled, isEnabled)
}
return nil
}
func (f *datatableFeature) theBooleanValuesShouldBeStoredCorrectly() error {
for i, isEnabled := range f.isEnabled {
fmt.Printf("Value %d: %v\n", i, isEnabled)
}
return nil
}
func FeatureContext(s *godog.Suite) {
f := &datatableFeature{}
s.Step(`^I have a datatable with boolean values$`, f.iHaveADataTableWithBooleanValues)
s.Step(`^the boolean values should be stored correctly$`, f.theBooleanValuesShouldBeStoredCorrectly)
}
func main() {
status := godog.TestSuite{
Name: "Gherkin Datatable",
ScenarioInitializer: FeatureContext,
Options: &godog.Options{
Format: "pretty",
Paths: []string{"datatable.feature"},
NoColors: false,
},
}.Run()
if status != 0 {
fmt.Println("Test failed")
}
}
Value 0: true
Value 1: false
Value 2: true
在上面的代码中,iHaveADataTableWithBooleanValues
函数将Gherkin中的数据表转换为Golang中的布尔值,并将其存储在isEnabled
切片中。theBooleanValuesShouldBeStoredCorrectly
函数用于验证布尔值是否正确存储。
这是一个基本的示例,你可以根据实际需求进行修改和扩展。请确保你已经按照godog
库的要求正确安装和配置。
领取专属 10元无门槛券
手把手带您无忧上云