dotnet / dotnet/aspnetcore

TraceId and similar should be a fixed value when Testing with WebApplicationFactory

Open
#61,207 3 comments 0 reactions 0 assignees View on GitHub
area-mvc feature-mvc-testing
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

When using `builder.Services.AddProblemDetails();` error output like 400 will have a `traceId` - which is useful.

```json
{
"type": "https://tools.ietf.org/html/rfc9110#section-15.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-9a91dea3f49537ea081001f1435df8f6-9a48ee11db3f47a3-00"
}
```

However when writing tests using `WebApplicationFactory` the returned `traceId` is _out of my control_ or at least not straight forward to set to a known value.

### Expected Behavior

`traceId` and similar tracking ids should be fixed or easily configurable when testing via WebApplicationFactory.

### Steps To Reproduce

1. Spin up a asp net web api
2. Add `builder.Services.AddProblemDetails();`
3. Make an endpoint return a bad request
4. Write a test that checks the returned data
5. Notice the traceId change

### Exceptions (if any)

_No response_

### .NET Version

9.0.202

### Anything else?

The easy workaround is to ignore `traceId` in the test. But that's not the point.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.