Benchmarks
These benchmarks compare the happy-path overhead of aweXpect.Json (passing assertions, the dominant case in a healthy test suite) against the equivalent FluentAssertions calls.
- Suite:
HappyCaseBenchmarksdriven by BenchmarkDotNet. - Job:
MediumRunwith the in-process toolchain. - Runner:
ubuntu-latestGitHub Actions agent, refreshed on every push tomain. - FluentAssertions has expectations on
JsonNode, but not on JSON strings orJsonElement. Where it lacks an expectation, its benchmark does what a FluentAssertions user writes instead, e.g. parse the string into aJsonNode. - Each tab shows the latest measured time and allocated memory; bars are scaled within the row so length corresponds to magnitude. Lower is better.
- JSON string
- JSON equality
- JSON string expectations
- Collection of JSON strings
- Parsed JSON
- Properties
- Combined expectations
- Nested properties
- Array elements
- Array in any order
Verifies that a JSON string matches an expected object.
FluentAssertions parses the string into a JsonNode.
await That(_jsonString).IsValidJsonMatching(new { title = "Let It Be", artist = "The Beatles", });
JsonNode.Parse(_jsonString).Should().BeEquivalentTo(new { title = "Let It Be", artist = "The Beatles", });
Verifies that a JSON string is equal to an expected JSON string with a different property order and formatting.
FluentAssertions cannot compare two JsonNodes with BeEquivalentTo, so it parses both strings and verifies the result of JsonNode.DeepEquals.
await That(_jsonString).IsEqualTo(_expectedJsonString).AsJson();
JsonNode.DeepEquals(JsonNode.Parse(_jsonString), JsonNode.Parse(_expectedJsonString)).Should().BeTrue();
Verifies that a JSON string is valid JSON whose title has the expected value.
FluentAssertions parses the string into a JsonNode.
await That(_jsonString).IsValidJson(json => json.HasProperty("title", t => t.IsString("Let It Be")));
JsonNode.Parse(_jsonString)!.Should().HaveProperty("title").Which.GetValue<string>().Should()
.Be("Let It Be");
Verifies that all 100 JSON strings of a collection are equal to an expected JSON string with a different property order and formatting.
FluentAssertions parses the expected string once and verifies the result of JsonNode.DeepEquals for every item.
await That(_jsonStrings).All().AreEqualTo(_expectedJsonString).AsJson();
JsonNode? expected = JsonNode.Parse(_expectedJsonString);
_jsonStrings.Should()
.AllSatisfy(jsonString => JsonNode.DeepEquals(JsonNode.Parse(jsonString), expected).Should().BeTrue());
Verifies that parsed JSON matches an expected object.
aweXpect verifies a JsonElement, FluentAssertions a JsonNode.
await That(_jsonElement).Matches(new { title = "Let It Be", artist = "The Beatles", });
_jsonNode.Should().BeEquivalentTo(new { title = "Let It Be", artist = "The Beatles", });
Verifies single properties of a JsonNode: the title has the expected value and the duration is a number.
await That(_jsonNode)
.HasProperty("title", t => t.IsString("Let It Be")).And
.HasProperty("durationSeconds", d => d.IsNumber());
_jsonNode.Should().HaveProperty("title").Which.GetValue<string>().Should().Be("Let It Be");
_jsonNode.Should().HaveProperty("durationSeconds").Which.Should().BeNumeric();
Verifies several expectations on the same JsonNode, combined with And: it has three properties, has no property album and is an object.
FluentAssertions has no expectation that a node is an object, so it verifies that it is no array instead.
await That(_jsonNode)
.HasProperty("title").And
.HasProperty("artist").And
.HasProperty("durationSeconds").And
.DoesNotHaveProperty("album").And
.IsObject();
_jsonNode.Should().HaveProperty("title");
_jsonNode.Should().HaveProperty("artist");
_jsonNode.Should().HaveProperty("durationSeconds");
_jsonNode.Should().NotHaveProperty("album");
_jsonNode.Should().NotBeAnArray();
Verifies that the object in a property of a JsonNode has three properties.
await That(_nestedJsonNode)
.HasProperty("track", track => track
.HasProperty("title").And
.HasProperty("artist").And
.HasProperty("durationSeconds"));
JsonNode track = _nestedJsonNode.Should().HaveProperty("track").Which;
track.Should().HaveProperty("title");
track.Should().HaveProperty("artist");
track.Should().HaveProperty("durationSeconds");
Verifies that all three elements of a JSON array match an expected object.
aweXpect verifies a JsonElement, FluentAssertions a JsonArray.
await That(_tracksElement).IsArray().WhoseElements
.All().ComplyWith(t => t.Matches(new { artist = "The Beatles", }));
_tracksNode.Should().AllSatisfy(t => t.Should().BeEquivalentTo(new { artist = "The Beatles", }));
Verifies that a JSON array of 10 titles matches the expected titles in reverse order.
aweXpect verifies a JsonElement, FluentAssertions a JsonNode, whose BeEquivalentTo ignores the order by default.
await That(_titlesElement).MatchesExactly(_reversedTitles, o => o.IgnoringArrayOrder());
_titlesNode.Should().BeEquivalentTo(_reversedTitles);