Let see some action now and jump into how to test simple function residing in a class.
For this we start with creating a simple class library in C# using Visual Studio 2008.
Lets proceed with putting some simple code snippet which provides us simple arithmetic operations.
Lets proceed with adding unit test project for this class library which will contain two unit test methods.
For this we right click on class name and click on choice “Create Unit Tests… “.
Following dialog is then displayed for user which allows some settings to be made as per user’s preferences.
Here we select class and both the methods inside class so that Unit Tests will be created for them.
Output project type is C# test project which user can select as per preference.
Additionally clicking “settings” button will open Test Generation Settings dialog which will allow to set Naming Settings as well as General settings.
There are five choices displayed for user under general settings.
- Mark all test results inconclusive by default
- Enable generation warnings
- Globally Qualify all types
- Enable documentation comments
- Honor InternalsVisibleTo Attribute.
There you go ! Last one needs to be paid attention to ..
For time being lets proceed with our unit tests for AddTwoNumbers and SubTwoNumbers methods.
Once we click ok user is prompted for name of the Test project.
Clicking of Create button will add Test project to the solution.
If we closely observe few things added newly in the project
- LocalTestRun.testrunconfig file
- Unit Testing.vsmdi file
- SimpleMathTest project which contains SimpleMathTest.cs file.
Lets see what all contents we have inside SimpleMathTest.cs file.
Object of TextContext class called testContextInstance.
get; set; for TextContext which provides information about and functionality for the current test run.
Next to this we have two test methods
we modify the values for “a” and “b” variables so as to check whether the actual and expected results match or not.
Changes made will look like this
What we re trying to judge here is after passing a as 10 and b as 10 , whether addition is returned as 20 and subtraction as 0.
But question remains.. How do I test this ?? Lets check it out…
As shown in the image, if we click on Test->Run we find two options
- Test in current context
- All Tests in solution
We select option Test in Current context. Another way to run Test in current context is Pressing Ctrl+R, T directly which will execute currently selected test case.
Another simplest way is to click on vsmdi file which will open up Test List Editor listing all the test cases. We can also use toolbar items to run the tests in current context or all tests in solution.
After clicking on Run Test in current context what we get is as follows
What we see here in bottom as test is passed. Since we passed 10,10 as two input values and 20 as expected result; both values matched and test case is marked as successful(passed).
Whether to check what happens when actual and expected values do not match lets try out changing the values and this time we do it in SubTwoNumbersTest.
We change the value of b as 20 but still expected result is 0 and this is not going to match with actual value.
This time we will run the test case by using option ctrl+R, T
The failed test case result will look like
We can also add columns to Test Results by right clicking first column header as per our preference.
So far we have used Asser.Equal to compare actual and expected result. But Assert class provides us various methods which can be used to conduct a test case. All the details for this methods can be found here.
Before we conclude lets quickly summarize what we have seen in this post
- How to add a unit test project
- Details of unit test project and how test functions are added.
- How to run test cases and ways to run test cases
- Details of Assert class and methods residing under it
In coming post we will see how “Honor Internals Visible To “ attribute is used and what happens with methods with different scope like private, internal and private static etc.
Please do write me about your feedback or queries which I am expecting the most , so that this articles can be made better and better !