Skip to content

Convert ASP.NET tests to use Snapshot Testing with VerifyTests/Verify#1454

Merged
andrewlock merged 15 commits into
masterfrom
andrew/verify
May 26, 2021
Merged

Convert ASP.NET tests to use Snapshot Testing with VerifyTests/Verify#1454
andrewlock merged 15 commits into
masterfrom
andrew/verify

Conversation

@andrewlock

@andrewlock andrewlock commented May 7, 2021

Copy link
Copy Markdown
Member

Note: The +/- line count is deceiving. If you exclude the snapshot files, there is a massive drop in lines of code, and an even bigger drop in number of characters 😄 e.g. this file is gone!

This initial PoC creates some helpers for verifying MockTracerAgent.Spans and converts our existing ASP.NET integration tests to use snapshot testing.

Challenges:

  • Needed to write a custom scrubber for token-replacement of IDs. This is pretty crude atm but it works. May be enabled OOTB in future Verify versions Done in Scrub numeric ids VerifyTests/Verify#351 and Add support for scrubbing nullable numeric IDs VerifyTests/Verify#352!
  • Verify doesn't support < .NET 4.6.1. That may mean it's not a viable option for every case given we need to support .NET 4.5.2 🙁
  • Ran into some weird errors due to trying to create filenames with / in them. Solved by stripping the /.
  • Multiple test classes in a file requires a bit of extra verbosity that won't be required in most cases
  • Running integration tests locally is still a royal pain ATM, which makes generating the initial verification text a bit of a pain. Obviously we should be able to do this though, and Nuke will help 😉
  • Had to do a bit of csproj fiddling to get the nesting to work, again because we have multiple test classes in a file in this case. Overall it works well though:
  • Different architectures apparently have different stack traces, so need to use different snapshots per architecture. We could just strip out the error entirely, but if this is stable, would be a better option I think. Native support should be coming soon to Verify.

image

Wins:

  • All the benefits of snapshot testing - easier to understand, tests more things, easier to understand failures
  • Easily plugs into existing tests by just replacing all the asserts with a single Verify method
  • Personally think this is a great option for bread-and-butter integration testing. Makes visualising traces a lot easier, and gives more complete coverage

Remaining questions:

  • How stable are these spans across platforms? well integration tests work, so there we go!
  • Need to make the environments deterministic - just noticed my system level DD_ENV=andrew has made its way into the span! Added explicit setting of DD_ENV for sample projects in 5c93c35. Can be overwritten, but should ensure consistency in other environments.
  • There appears to be an issue with the solution path scrubbing, which is causing the current failures in the integration tests pipeline. The attributes don't appear to be embedded in the integration test project for some reason. This is probably something to do with our convoluted CI build process (as it works in VS) but I'm not sure why... 🤞 Fixed in de6965c, it's because $(SolutionDir) isn't set when calling dotnet build on a project as opposed to the solution.
  • Need to check how Verify handles reordering in lists/dictionaries

@andrewlock
andrewlock marked this pull request as ready for review May 21, 2021 11:45
@andrewlock
andrewlock requested a review from a team as a code owner May 21, 2021 11:45
@andrewlock andrewlock changed the title Initial PoC for switching to VerifyTests/Verify Convert ASP.NET tests to use VerifyTests/Verify May 21, 2021
@andrewlock andrewlock changed the title Convert ASP.NET tests to use VerifyTests/Verify Convert ASP.NET tests to use Snapshot Testing with VerifyTests/Verify May 21, 2021
@andrewlock
andrewlock force-pushed the andrew/verify branch 5 times, most recently from d310573 to af90959 Compare May 25, 2021 13:41

// Overriding the type name here as we have multiple test classes in the file
// Ensures that we get nice file nesting in Solution Explorer
await Verifier.Verify(spans, settings)

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.

Can we also set up Verify to place all snapshots in its own directory? I'm thinking something like /test/snapshots/<ProjectName>/**/verified.txt

@andrewlock andrewlock May 26, 2021

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Well, we could yes. And for this example, that might make sense because there are so many variations, but in general I think the "default" behaviour is preferable. Having the snapshots stored "with" the test itself makes it easier to manually check them and for referring back and forth I think. We shouldn't have to do all this customisation in most cases.

I'm open to what people think though - it looks messier in file browsers, but in VS/Rider, having the snapshots folded under your test class is preferable I think, as you can easily jump to individual snapshots, but they're hidden by default:

Image folding

}

public static TheoryData<string, int> Data() => new()

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.

This is so nice, we just need to specify the inputs and ensure the output is expected 😍

@zacharycmontoya zacharycmontoya left a comment

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.

LGTM. Let's verify all the things!

@tonyredondo tonyredondo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM nice!

@lucaspimentel lucaspimentel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Big step forward for our testing suite, thanks! Left a couple of non-blocking comments.


public AspNetWebApi2Tests(IisFixture iisFixture, ITestOutputHelper output, bool enableCallTarget, bool classicMode)
public AspNetWebApi2Tests(IisFixture iisFixture, ITestOutputHelper output, bool enableCallTarget, bool classicMode, bool enableFeatureFlag)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Consider renaming parameter enableFeatureFlag to something more obvious (which feature flag?). Something like enableRouteTemplateResourceNames (but shorter?). Thanks!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done in 20bec17

+ (enableCallTarget ? ".CallSite" : ".CallTarget")
+ (classicMode ? ".Classic" : ".Integrated")
+ (enableFeatureFlag ? ".NoFF" : ".WithFF")
+ (RuntimeInformation.ProcessArchitecture == Architecture.X64 ? ".X64" : ".X86"); // assume that arm is the same

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm in two minds. Will the call stack be different on Arm? Maybe not? Might not be necessary? I'm inclined to punt until we do run this on ARM (Windows Arm, I guess it's just a matter of time?)

@lucaspimentel lucaspimentel added the area:tests unit tests, integration tests label May 26, 2021
andrewlock added 14 commits May 26, 2021 15:11
These should never be checked in (in contrast to *.verified.* files
Json.NET update required as Verify depends on 13.0.1
Lets us use C# 9 Module Initializers on < .NET 5
We may need to add additional scrubbers etc.
In addition to the built-in scrubbers, I'm
- Excluding the variable duration/start fields
- Fixing localhost ports to all be :0000
- Fixing the _dd.tracer_kr: 1.0

Also adds helper for fetching the web spans (without asserting), and for sanitising paths for use in filenames
The stack traces are different depending on architecture.
There's a PR in to Verify to support this OOTB: VerifyTests/Verify#367
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:tests unit tests, integration tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants