ExpressionToCode alternatives and similar packages
Based on the "Testing" category.
Alternatively, view ExpressionToCode alternatives based on common mentions on social networks and blogs.
-
Bogus
:card_index: A simple fake data generator for C#, F#, and VB.NET. Based on and ported from the famed faker.js. -
Moq
Repo for managing Moq 4.x [Moved to: https://github.com/moq/moq] -
xUnit
xUnit.net is a free, open source, community-focused unit testing tool for .NET. -
Fluent Assertions
A very extensive set of extension methods that allow you to more naturally specify the expected outcome of a TDD or BDD-style unit tests. Targets .NET Framework 4.7, as well as .NET Core 2.1, .NET Core 3.0, .NET 6, .NET Standard 2.0 and 2.1. Supports the unit test frameworks MSTest2, NUnit3, XUnit2, MSpec, and NSpec3. -
SpecFlow
#1 .NET BDD Framework. SpecFlow automates your testing & works with your existing code. Find Bugs before they happen. Behavior Driven Development helps developers, testers, and business representatives to get a better understanding of their collaboration -
AutoFixture
AutoFixture is an open source library for .NET designed to minimize the 'Arrange' phase of your unit tests in order to maximize maintainability. Its primary goal is to allow developers to focus on what is being tested rather than how to setup the test scenario, by making it easier to create object graphs containing test data. -
Testcontainers
A library to support tests with throwaway instances of Docker containers for all compatible .NET Standard versions. -
NSubstitute
A friendly substitute for .NET mocking libraries. -
Shouldly
Should testing for .NET—the way assertions should be! -
Verify
Verify is a snapshot tool that simplifies the assertion of complex data models and documents. -
NBomber
Modern and flexible load testing framework for Pull and Push scenarios, designed to test any system regardless a protocol (HTTP/WebSockets/AMQP etc) or a semantic model (Pull/Push). -
Stryker.NET
Mutation testing for .NET core and .NET framework! -
WireMock.Net
WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality. -
Compare-Net-Objects
What you have been waiting for :+1: Perform a deep compare of any two .NET objects using reflection. Shows the differences between the two objects. -
NetArchTest
A fluent API for .Net that can enforce architectural rules in unit tests. -
Machine.Specifications
Machine.Specifications is a Context/Specification framework for .NET that removes language noise and simplifies tests. -
GenFu
GenFu is a library you can use to generate realistic test data. It is composed of several property fillers that can populate commonly named properties through reflection using an internal database of values or randomly created data. You can override any of the fillers, give GenFu hints on how to fill them. -
Expecto
A smooth testing lib for F#. APIs made for humans! Strong testing methodologies for everyone! -
ArchUnitNET
A C# architecture test library to specify and assert architecture rules in C# for automated testing. -
Canopy
f# web automation and testing library, built on top of Selenium (friendly to c# also) -
Fine Code Coverage
Visualize unit test code coverage easily for free in Visual Studio Community Edition (and other editions too) -
FluentAutomation
Simple Fluent API for UI Automation -
NFluent
Smooth your .NET TDD experience with NFluent! NFluent is an ergonomic assertion library which aims to fluent your .NET TDD experience (based on simple Check.That() assertion statements). NFluent aims your tests to be fluent to write (with a super-duper-happy 'dot' auto-completion experience), fluent to read (i.e. as close as possible to plain English expression), but also fluent to troubleshoot, in a less-error-prone way comparing to the classical .NET test frameworks. NFluent is also directly inspired by the awesome Java FEST Fluent assertion/reflection library (http://fest.easytesting.org/) -
xBehave.net
✖ An xUnit.net extension for describing each step in a test with natural language. -
LightBDD
BDD framework allowing to create easy to read and maintain tests. -
NSpec
A battle hardened testing framework for C# that's heavily inspired by Mocha and RSpec. -
SpecsFor
SpecsFor is a light-weight Behavior-Driven Development framework that focuses on ease of use for *developers* by minimizing testing friction. -
snapshooter
Snapshooter is a snapshot testing tool for .NET Core and .NET Framework -
Xunit.Gherkin.Quick
BDD in .NET Core - using Xunit and Gherkin (compatible with both .NET Core and .NET) -
SimpleStubs
*SimpleStubs* is a simple mocking framework that supports Universal Windows Platform (UWP), .NET Core and .NET framework. SimpleStubs is currently developed and maintained by Microsoft BigPark Studios in Vancouver. -
Moq.Contrib.HttpClient
A set of extension methods for mocking HttpClient and IHttpClientFactory with Moq. -
NScenario
Dead simple library for annotating steps of test case scenarios. -
#<Sawyer::Resource:0x00007f89811b9240>
:fire: A small library to help .NET developers leverage Microsoft's dependency injection framework in their Xunit-powered test projects -
TestContainers dotnet
dotnet port of testcontainers-java -
SecTester
SecTester is a new tool that integrates our enterprise-grade scan engine directly into your unit tests.
InfluxDB - Power Real-Time Data Analytics at Scale
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of ExpressionToCode or a related project?
README
ExpressionToCode
ExpressionToCode generates valid, readable C# from an Expression Tree. (nuget: ExpressionToCodeLib)
An example:
ExpressionToCode.ToCode(
() => new[] { 1.0, 2.01, 3.5 }.SequenceEqual(new[] { 1.0, 2.01, 3.5 })
)
== "() => new[] { 1.0, 2.01, 3.5 }.SequenceEqual(new[] { 1.0, 2.01, 3.5 })"
ExpressionToCode also provides something like Groovy's Power Assert which includes the code of the failing assertion's expression and the values of its subexpressions. This functionality is particularly useful in a unit testing framework such as NUnit or xUnit.NET. When you execute the following (failing) assertion:
PAssert.That(()=>Enumerable.Range(0,1000).ToDictionary(i=>"n"+i)["n3"].ToString()==(3.5).ToString());
The assertion fails with the following message:
assertion failed
Enumerable.Range(0, 1000).ToDictionary(i => "n" + i)["n3"].ToString(CultureInfo.InvariantCulture) == 3.5.ToString(CultureInfo.InvariantCulture)
→ false (caused assertion failure)
Enumerable.Range(0, 1000).ToDictionary(i => "n" + i)["n3"].ToString(CultureInfo.InvariantCulture)
→ "3"
Enumerable.Range(0, 1000).ToDictionary(i => "n" + i)["n3"]
→ 3
Enumerable.Range(0, 1000).ToDictionary(i => "n" + i)
→ new Dictionary<string, int> {
["n0"] = 0,
["n1"] = 1,
["n2"] = 2,
["n3"] = 3,
["n4"] = 4,
["n5"] = 5,
["n6"] = 6,
["n7"] = 7,
["n8"] = 8,
["n9"] = 9,
["n10"] = 10,
["n11"] = 11,
["n12"] = 12,
["n13"] = 13,
["n14"] = 14,
["n15"] = 15,
["n16"] = 16,
["n17"] = 17,
["n18"] = 18,
["n19"] = 19,
["n20"] = 20,
["n21"] = 21,
["n22"] = 22,
["n23"] = 23,
["n24"] = 24,
["n25"] = 25,
["n26"] = 26,
["n27"] = 27,
["n28"] = 28,
["n29"] = 29,
...
}
Enumerable.Range(0, 1000)
→ { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, ... }
3.5.ToString(CultureInfo.InvariantCulture)
→ "3.5"
ExpressionToCode's output is configurable in various ways. For expressions with small values, a values-on-stalks rendering might instead be used:
var a = 2;
var b = 5;
ExpressionToCodeConfiguration.DefaultAssertionConfiguration.WithAnnotator(CodeAnnotators.ValuesOnStalksCodeAnnotator)
.Assert(() => Math.Max(a, b) > new[] { 3, 8, 13, 4 }.Average() );
Math.Max(a, b) > new[] { 3, 8, 13, 4 }.Average() : assertion failed
│ │ │ │ │
│ │ │ │ 7.0
│ │ │ new[] { 3, 8, 13, 4 }
│ │ 5
│ 2
5
Note that the default configuration for asserts (i.e. PAssert.That
) limits the length of sequences and strings; the default configuration of code-generation does not.
ExpressionToCode was inspired by Power Assert.NET. It differs from PowerAssert.NET by supporting a larger portion of the lambda syntax and that the generated C# is more frequently valid; the aim is to generate valid C# for all expression trees created from lambda's. Currently supported:
Expression tree support
- Supports static field and property access
- Supports more operators, e.g. logical and bitwise negation
- Recognizes C# indexer use (e.g.
dict["mykey"]==3
), in addition to special cases for array indexers and string indexers - Adds parentheses where required by operator precedence and associativity (e.g.
() => x - (a - b) + x * (a + b)
is correctly regenerated) - Generates valid numeric and other constant literals including escapes and suffixes where required (e.g.
1m + (decimal)Math.Sqrt(1.41)
) - Supports C# syntactic sugar for object initializers, object member initializers, list initializers, extension methods, anonymous types (issues [#12](/../../issues/12), [#3](/../../issues/3)), etc
- Uses the same spacing rules Visual Studio does by default
- Supports nested Lambdas
- Expands generic type instances and nullable types into normal C# (e.g.
Func<int, bool>
andint?
) - Recognizes references to
this
and omits the keyword where possible ([#5](/../../issues/5)) - Recognizes closed-over variables and prints something plausible, rather than the crypic compiler-generated names.
- Omits most implicit casts (e.g.
object.Equals(3, 4)
instead ofobject.Equals((object)3, (object)4)
) - user defined implicit cast operators are not elided ([#4](/../../issues/4)) - Detects when type parameters to methods are superfluous ([#13](/../../issues/13)).
Not implemented (yet?):
- Use LINQ query syntax where possible - issue [#6](/../../issues/6).
- Explicitly cast otherwise inferable lambda when required due to ambiguous overloads - issue [#14](/../../issues/14).
- Warn when
==
differs from.Equals
or.SequenceEquals
, as Power Assert.NET does (issue [#2](/../../issues/2)). - See all open issues.
ExpressionToCode
API
All classes live in the ExpressionToCodeLib
namespace.
These are:
PAssert
for making assertions in NUnit tests and elsewhere.PAssert.That
andPAssert.IsTrue
are identical; both test the provided boolean expression and print a readable error message on failure
ExpressionToCode
Renders a System.Linq.Expressions.Expression object to source code.ExpressionToCode.ToCode
(several overloads) simply renders the expression as source code.ExpressionToCode.AnnotatedToCode
(several overloads) renders the expression as source code, then annotates all subexpressions which are computable with their value using the stalk-like rendering as shown on the Project Home page.
Two public helper classes exist:
PAssertFailedException
thrown on assertion failure.ObjectToCode
Renders .NET objects to code; a helper class.ObjectToCode.PlainObjectToCode
renders simple objects that can be parsed by the C# compiler. This includes strings, chars, decimals, floats, doubles, all the integer types, booleans, enums, nulls, and default struct values.ObjectToCode.ComplexObjectToPseudoCode
renders as best it can anything thrown at it; but the resultant rendering is not necessarily compilable. This is used to display the values of subexpressions.
A complete listing of the public api is [here](ExpressionToCodeTest/ApiStabilityTest.PublicApi.approved.txt)
Supported platforms
Requires .NET 4.5.2 or .net standard 1.6 (previous versions support older platforms)
If you have any questions, you can contact me via github or mail eamon at nerbonne dot org.
See the documentation above, then download from or import using NuGet, or just checkout the source (license: Apache 2.0 or the MIT license, at your option)!
*Note that all licence references and agreements mentioned in the ExpressionToCode README section above
are relevant to that project's source code only.