♻️ refactor: make BDD test setup DRY with shared testsetup package

- Create pkg/bdd/testsetup package with shared test configuration functions
- Refactor all feature test files to use shared setup (70+ lines reduced)
- Implement dynamic feature path detection by scanning filesystem for directories
- Add getProjectRoot() function to find project root via go.mod
- Maintain all existing functionality (tags, stop on failure, etc.)
- Add fallback to hardcoded paths if filesystem access fails
- Sort feature paths for consistent test execution order

Before: ~35 lines per test file with duplicated setup code
After: ~5 lines per test file using shared functions

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistralai.com>
This commit is contained in:
2026-04-10 10:24:25 +02:00
parent 756fc5abfd
commit aa4823eb11
7 changed files with 215 additions and 210 deletions

View File

@@ -1,45 +1,14 @@
package auth
import (
"os"
"testing"
"dance-lessons-coach/pkg/bdd"
"github.com/cucumber/godog"
"dance-lessons-coach/pkg/bdd/testsetup"
)
func TestAuthBDD(t *testing.T) {
// Set FEATURE environment variable for feature-specific configuration
os.Setenv("FEATURE", "auth")
// Allow tag override via environment variable
tags := os.Getenv("GODOG_TAGS")
if tags == "" {
// Default tags if not overridden
tags = "~@flaky && ~@todo && ~@skip"
}
// Allow stop on failure override via environment variable
stopOnFailure := true // Default for auth tests
if envStop := os.Getenv("GODOG_STOP_ON_FAILURE"); envStop != "" {
stopOnFailure = envStop == "true" || envStop == "1"
}
suite := godog.TestSuite{
Name: "dance-lessons-coach BDD Tests - Auth Feature",
TestSuiteInitializer: bdd.InitializeTestSuite,
ScenarioInitializer: bdd.InitializeScenario,
Options: &godog.Options{
Format: "progress",
Paths: []string{"."},
TestingT: t,
Strict: true,
Randomize: -1,
StopOnFailure: stopOnFailure,
Tags: tags,
},
}
config := testsetup.NewFeatureConfig("auth", "progress", true)
suite := testsetup.CreateTestSuite(t, config, "dance-lessons-coach BDD Tests - Auth Feature")
if suite.Run() != 0 {
t.Fatal("non-zero status returned, failed to run auth BDD tests")

View File

@@ -1,64 +1,30 @@
package features
import (
"os"
"testing"
"dance-lessons-coach/pkg/bdd"
"github.com/cucumber/godog"
"dance-lessons-coach/pkg/bdd/testsetup"
)
func TestBDD(t *testing.T) {
// Get feature name from environment variable or default to all features
feature := os.Getenv("FEATURE")
feature := testsetup.GetFeatureFromEnv()
var paths []string
var suiteName string
var paths []string
if feature == "" {
// Run all features
suiteName = "dance-lessons-coach BDD Tests - All Features"
paths = []string{
"auth",
"config",
"greet",
"health",
"jwt",
}
paths = testsetup.GetAllFeaturePaths()
} else {
// Run specific feature
suiteName = "dance-lessons-coach BDD Tests - " + feature + " Feature"
paths = []string{feature}
}
// Allow tag override via environment variable
tags := os.Getenv("GODOG_TAGS")
if tags == "" {
// Default tags if not overridden
tags = "~@flaky && ~@todo && ~@skip"
}
// Allow stop on failure override via environment variable
stopOnFailure := false // Default for all features test (don't stop on failure)
if envStop := os.Getenv("GODOG_STOP_ON_FAILURE"); envStop != "" {
stopOnFailure = envStop == "true" || envStop == "1"
}
suite := godog.TestSuite{
Name: suiteName,
TestSuiteInitializer: bdd.InitializeTestSuite,
ScenarioInitializer: bdd.InitializeScenario,
Options: &godog.Options{
Format: "progress",
Paths: paths,
TestingT: t,
Strict: true,
Randomize: -1,
StopOnFailure: stopOnFailure,
Tags: tags,
},
}
config := testsetup.NewMultiFeatureConfig(paths, "progress", false)
suite := testsetup.CreateMultiFeatureTestSuite(t, config, suiteName)
if suite.Run() != 0 {
t.Fatal("non-zero status returned, failed to run BDD tests")

View File

@@ -1,45 +1,14 @@
package config
import (
"os"
"testing"
"dance-lessons-coach/pkg/bdd"
"github.com/cucumber/godog"
"dance-lessons-coach/pkg/bdd/testsetup"
)
func TestConfigBDD(t *testing.T) {
// Set FEATURE environment variable for feature-specific configuration
os.Setenv("FEATURE", "config")
// Allow tag override via environment variable
tags := os.Getenv("GODOG_TAGS")
if tags == "" {
// Default tags if not overridden
tags = "~@flaky && ~@todo && ~@skip"
}
// Allow stop on failure override via environment variable
stopOnFailure := false // Default for config tests (don't stop on failure)
if envStop := os.Getenv("GODOG_STOP_ON_FAILURE"); envStop != "" {
stopOnFailure = envStop == "true" || envStop == "1"
}
suite := godog.TestSuite{
Name: "dance-lessons-coach BDD Tests - Config Feature",
TestSuiteInitializer: bdd.InitializeTestSuite,
ScenarioInitializer: bdd.InitializeScenario,
Options: &godog.Options{
Format: "progress",
Paths: []string{"."},
TestingT: t,
Strict: true,
Randomize: -1,
StopOnFailure: stopOnFailure,
Tags: tags,
},
}
config := testsetup.NewFeatureConfig("config", "progress", false)
suite := testsetup.CreateTestSuite(t, config, "dance-lessons-coach BDD Tests - Config Feature")
if suite.Run() != 0 {
t.Fatal("non-zero status returned, failed to run config BDD tests")

View File

@@ -1,45 +1,14 @@
package greet
import (
"os"
"testing"
"dance-lessons-coach/pkg/bdd"
"github.com/cucumber/godog"
"dance-lessons-coach/pkg/bdd/testsetup"
)
func TestGreetBDD(t *testing.T) {
// Set FEATURE environment variable for feature-specific configuration
os.Setenv("FEATURE", "greet")
// Allow tag override via environment variable
tags := os.Getenv("GODOG_TAGS")
if tags == "" {
// Default tags if not overridden
tags = "~@flaky && ~@todo && ~@skip"
}
// Allow stop on failure override via environment variable
stopOnFailure := true // Default for greet tests
if envStop := os.Getenv("GODOG_STOP_ON_FAILURE"); envStop != "" {
stopOnFailure = envStop == "true" || envStop == "1"
}
suite := godog.TestSuite{
Name: "dance-lessons-coach BDD Tests - Greet Feature",
TestSuiteInitializer: bdd.InitializeTestSuite,
ScenarioInitializer: bdd.InitializeScenario,
Options: &godog.Options{
Format: "progress",
Paths: []string{"."},
TestingT: t,
Strict: true,
Randomize: -1,
StopOnFailure: stopOnFailure,
Tags: tags,
},
}
config := testsetup.NewFeatureConfig("greet", "progress", true)
suite := testsetup.CreateTestSuite(t, config, "dance-lessons-coach BDD Tests - Greet Feature")
if suite.Run() != 0 {
t.Fatal("non-zero status returned, failed to run greet BDD tests")

View File

@@ -1,45 +1,14 @@
package health
import (
"os"
"testing"
"dance-lessons-coach/pkg/bdd"
"github.com/cucumber/godog"
"dance-lessons-coach/pkg/bdd/testsetup"
)
func TestHealthBDD(t *testing.T) {
// Set FEATURE environment variable for feature-specific configuration
os.Setenv("FEATURE", "health")
// Allow tag override via environment variable
tags := os.Getenv("GODOG_TAGS")
if tags == "" {
// Default tags if not overridden
tags = "~@flaky && ~@todo && ~@skip"
}
// Allow stop on failure override via environment variable
stopOnFailure := true // Default for health tests
if envStop := os.Getenv("GODOG_STOP_ON_FAILURE"); envStop != "" {
stopOnFailure = envStop == "true" || envStop == "1"
}
suite := godog.TestSuite{
Name: "dance-lessons-coach BDD Tests - Health Feature",
TestSuiteInitializer: bdd.InitializeTestSuite,
ScenarioInitializer: bdd.InitializeScenario,
Options: &godog.Options{
Format: "progress",
Paths: []string{"."},
TestingT: t,
Strict: true,
Randomize: -1,
StopOnFailure: stopOnFailure,
Tags: tags,
},
}
config := testsetup.NewFeatureConfig("health", "progress", true)
suite := testsetup.CreateTestSuite(t, config, "dance-lessons-coach BDD Tests - Health Feature")
if suite.Run() != 0 {
t.Fatal("non-zero status returned, failed to run health BDD tests")

View File

@@ -1,45 +1,14 @@
package jwt
import (
"os"
"testing"
"dance-lessons-coach/pkg/bdd"
"github.com/cucumber/godog"
"dance-lessons-coach/pkg/bdd/testsetup"
)
func TestJWTBDD(t *testing.T) {
// Set FEATURE environment variable for feature-specific configuration
os.Setenv("FEATURE", "jwt")
// Allow tag override via environment variable
tags := os.Getenv("GODOG_TAGS")
if tags == "" {
// Default tags if not overridden
tags = "~@flaky && ~@todo && ~@skip"
}
// Allow stop on failure override via environment variable
stopOnFailure := true // Default for JWT tests
if envStop := os.Getenv("GODOG_STOP_ON_FAILURE"); envStop != "" {
stopOnFailure = envStop == "true" || envStop == "1"
}
suite := godog.TestSuite{
Name: "dance-lessons-coach BDD Tests - JWT Feature",
TestSuiteInitializer: bdd.InitializeTestSuite,
ScenarioInitializer: bdd.InitializeScenario,
Options: &godog.Options{
Format: "pretty",
Paths: []string{"."},
TestingT: t,
Strict: true,
Randomize: -1,
StopOnFailure: stopOnFailure,
Tags: tags,
},
}
config := testsetup.NewFeatureConfig("jwt", "pretty", true)
suite := testsetup.CreateTestSuite(t, config, "dance-lessons-coach BDD Tests - JWT Feature")
if suite.Run() != 0 {
t.Fatal("non-zero status returned, failed to run jwt BDD tests")