Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion internal/framework/pytest.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func (p *PyTest) DiscoverTests(ctx context.Context, testFiles discovery.TestFile
}

args := []string{"-m", "pytest"}
command := "python"
if len(p.commandOverride) > 0 {
command = p.commandOverride[0]
args = p.commandOverride[1:]
}

if testFiles.UseExplicitFiles() {
args = append(args, testFiles.ExplicitFiles...)
Expand All @@ -91,7 +96,7 @@ func (p *PyTest) DiscoverTests(ctx context.Context, testFiles discovery.TestFile
args = append(args, files...)
}

return discovery.DiscoverTests(ctx, p.executor, "python", args, p.platformEnv)
return discovery.DiscoverTests(ctx, p.executor, command, args, p.platformEnv)
}

func (p *PyTest) DiscoverTestFiles(ctx context.Context, testFiles discovery.TestFileSet) ([]string, error) {
Expand Down Expand Up @@ -128,6 +133,10 @@ func (p *PyTest) HasUnskippableMarker(testFile string) bool {
func (p *PyTest) RunTests(ctx context.Context, testFiles []string, envMap map[string]string) error {
command := "python"
args := []string{"-m", "pytest"}
if len(p.commandOverride) > 0 {
command = p.commandOverride[0]
args = p.commandOverride[1:]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Parallel workers share override argument storage

Some tests can run twice while other tests do not run.

Assertion details
  • Input: Use parallel workers with a multi-part override such as uv run pytest when the override slice has spare capacity.
  • Expected: Each worker must use a private argument slice. Clone the override arguments in RunTests before append. Apply the same safe handling in DiscoverTests, and add a multi-part parallel override test.
  • Actual: args shares the command-override backing array. Concurrent workers append different test files to this shared storage and can replace each other's arguments.

Was this helpful? React 👍 or 👎
🤖 Datadog Autotest · What is Autotest? · @DataDog review to ask questions · Any feedback? Reach out in #autotest

}
slog.Info("Running tests with command", "command", command, "args", args)
args = append(args, testFiles...)

Expand Down
72 changes: 72 additions & 0 deletions internal/framework/pytest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,78 @@ func TestPyTest_SupportsFullTestDiscovery(t *testing.T) {
}
}

func TestPyTest_RunTests_WithCommandOverride(t *testing.T) {
testFiles := []string{"tests/test_user.py"}

var capturedName string
var capturedArgs []string
mockExecutor := &mockCommandExecutor{
capturedEnvMap: map[string]string{},
onExecution: func(name string, args []string) {
capturedName = name
capturedArgs = args
},
}

pytest := &PyTest{
executor: mockExecutor,
platformEnv: map[string]string{},
commandOverride: []string{"pytest"},
}
if err := pytest.RunTests(context.Background(), testFiles, nil); err != nil {
t.Fatalf("RunTests failed: %v", err)
}

if capturedName != "pytest" {
t.Fatalf("expected command 'pytest' (from commandOverride), got %q", capturedName)
}
expectedArgs := []string{"tests/test_user.py"}
if !slices.Equal(capturedArgs, expectedArgs) {
t.Fatalf("expected args %v (no -m pytest), got %v", expectedArgs, capturedArgs)
}
}

func TestPyTest_DiscoverTests_WithCommandOverride(t *testing.T) {
if err := os.MkdirAll(filepath.Dir(discovery.TestsFilePath), 0755); err != nil {
t.Fatalf("failed to create discovery dir: %v", err)
}
defer cleanupDiscoveryDir()

explicitFiles := []string{"tests/test_foo.py"}
var capturedName string
var capturedArgs []string
mockExecutor := &mockCommandExecutor{
onExecution: func(name string, args []string) {
capturedName = name
capturedArgs = args
f, _ := os.Create(discovery.TestsFilePath)
_ = f.Close()
},
}

pytest := &PyTest{
executor: mockExecutor,
platformEnv: map[string]string{},
commandOverride: []string{"pytest"},
}
testFiles := discovery.TestFileSet{ExplicitFiles: explicitFiles}
_, _ = pytest.DiscoverTests(context.Background(), testFiles)

if capturedName != "pytest" {
t.Fatalf("expected command 'pytest' (from commandOverride), got %q", capturedName)
}
for _, expected := range explicitFiles {
if !slices.Contains(capturedArgs, expected) {
t.Errorf("expected file %q in pytest args, got %v", expected, capturedArgs)
}
}
for _, unexpected := range []string{"-m", "pytest"} {
if slices.Contains(capturedArgs, unexpected) {
t.Errorf("unexpected arg %q in pytest args (commandOverride should not add -m pytest)", unexpected)
}
}
}

func TestPyTest_RunTests(t *testing.T) {
testFiles := []string{"tests/test_user.py", "tests/test_auth.py"}
envMap := map[string]string{
Expand Down
Loading