Unit testing in C#
See the main documentation of the WebSharper.Testing library for F# here.
C# has all the same features, but the API is slightly different.
Test categories are classes, cases are marked with attributes, and a code generator is used to run all the tests in the project.
Categories and tests
TestCategory
TestCategory is a class which you can inherit from to create a class that houses a number of tests.
It has many methods that you can use in your test methods to define assertions.
Optionally, you can add a Test attribute with a string argument to name the category (default name is the class' name).
Example:
Test
The Test attribute can be used on methods too, to mark them as a test case.
Optionally, you can provide a string argument to name the test (default name is the method's name).
The method must follow these rules:
- It must be returning either
voidorTask. - It must be an instance method. A new instance of the containing class will be created for running each test.
- It can have none or a single parameter, in the latter case, random values will be created for it based on the type of the parameter.
Supported types are
int,double,bool,string,objectand also tuples, arrays,IEnumerableandListmade from these. Using theobjecttype results in values from mix of various types. When using a non-supported type, it results in a compile-time error.
Example:
TestGenerator
You must include a method like this in one of your classes:
This runs a compile-time generator, to create a JavaScript body for this method that runs all tests discovered in the current project.
It returns an IControlBody, which can be used inside the client (() => ...) helper to serve a page that runs the given tests,
or call its ReplaceInDom method directly on the client, to replace a placeholder DOM element to the content generated by QUnit.
Example:
Later, in server-side code, serve this on a sitelet endpoint:
Assertion functions
See the F# API for available assertion functions.
In C#, they are methods of the TestCategory class, which you can call from your test methods.
Main differences are:
- The method names are PascalCase instead of camelCase.
- There are no
...Msgvariants, instead there are overloaded versions of the same method that take an additionalstringargument for a test message. - There are no
...Asyncvariants, instead you can use theawaitkeyword in your test method that return aTask. - Note: the assertion checking for exceptions is using F# terminology so it is called
Raiseseven for C#.
Examples
Expect
Using Expect to specify the number of tests, including Expect(0):
IsTrue and IsFalse
Using IsTrue and IsFalse to check boolean expressions:
Equal and JsEqual
Equal checks that correspond to the == operator in C#.
While JsEqual checks that correspond to the == operator in JavaScript.
Raises
Raises takes a lambda expression so that it can check if it throws an exception.
Using randomly generated values
Random values can be used in your tests by using the RandomValues class, same as in F#.