This repo contains the code for, and results of, a series of performance benchmarks running against various .NET mocking libraries, using BenchmarkDotNet.
The baseline is a simple stub class.
Want to add more? PRs welcome!
- Add a method named after the framework to
IMockingBenchmarkIMockingBenchmark<T>MockingBenchmarkMockingBenchmark<T>
- Implement it in each of classes in the
Benchmarksdirectory - Add a reference to it in
All.Frameworks
PCLMock is a little different than the other libraries tested here, in that it requires explicitly generated mock classes. Compared against the other contenders, which use underlying parts of the framework like reflection to mock classes more "on the fly", PCLMock boasts improved performance over its more dynamic counterparts, at the cost of some additional effort during development time.
These tests cover standard mocking framework functionality
| Test | How long does it take... |
|---|---|
| Construction | for a mock to be created? |
| Return | for a mocked method to return a value? |
| EmptyReturn | for a mocked method to return default? |
| EmptyMethod | for a mocked method to be called? |
| OneParameter | for a mocked method to be called with a parameter? |
| Callback | for a mocked method to perform a callback? |
| Verify | for verification that a method was called? |
Want to add more? PRs welcome! Add a new class extending MockingBenchmark or MockingBenchmark<T>, depending on what you're testing, and a test case to BenchmarkTests with the relevant assertion.
Latest official results from the GitHub Actions workflow are available on the Results page.
- .NET SDK
- Run
dotnet restoreto install all dependent libraries and prep for build - Run
dotnet publish -c Releaseto build the benchmarks (the-c Releaseflag is important to maximize accuracy)
- Once restored and built, run
dotnet bin/Release/net9.0/publish/BenchmarkMockNet.dllto execute the benchmarks - Benchmarks will take about 5 minutes to run
- Results are stored in the
BenchmarkDotNet.Artifactsdirectory, in both HTML and Markdown formats