In the previous articleArticleUsing the mono runtime, the two simple consolesProgramIt is deployed on Linux and runs smoothly. In the agile age, testing is essential. Next let's look at the unit test solution on mono.
Nunit
It is best to run the test on the Linux platform, that is, the support of monodevloper is required. The built-in unit test framework of vs2010 is useless. One of the alternatives is the well-known nunit. Mono supports nunit running. The corresponding nunit Project template is also available in monodeveloper.
Simple use of nunit:
Nunit uses attribute to mark test cases:
Attribute in nunit
| Testfixture |
Mark unit test class |
| Test |
Identify Test Method |
| Testfixturesetup |
Test class initialization method |
| Testfixtureteardown |
Release the resource of the initialization method |
| Setup |
Test case initialization Function |
| Teardown |
Release Test Case Resources |
Configure nunit in vs 2010
Search for the "visual nunit 2010" plug-in on the extended manager in vs2010. This is an nunit runner.
After installation, you can open the plug-in Interface in "View"-"" other Windows "(or press Ctrl + F7)
We will continue to use generateprimes in the previous article as an example. Create a class library project in the solution and add the following unit testsCode:
Testcase
1 [Testfixture] 2 Public Class Generateprimestest 3 { 4 5 6 7 8 [Test] 9 Public Void Testnullprimes () 10 { 11 Int [] Nullarray = generateprimes. primegenerator. generateprimenumbers ( 0 ); 12 Assert. areequal (nullarray. length,0 ); 13 } 14 15 [Test] 16 Public Void Testminprimes () 17 { 18 Int [] Minarray = generateprimes. primegenerator. generateprimenumbers ( 2 ); 19 Assert. areequal (minarray. length, 1 ); 20 Assert. areequal (minarray [ 0 ], 2 ); 21 } 22 23 [Test] 24 Public Void Testthreeprimes () 25 { 26 Int [] Threearray = generateprimes. primegenerator. generateprimenumbers ( 3 ); 27 Assert. areequal (threearray. length, 2 ); 28 Assert. areequal (threearray [ 0 ], 2 ); 29 Assert. areequal (threearray [ 1 ], 3 ); 30 } 31 32 [Test] 33 Public Void Testcentprimes () 34 { 35 Int [] Centarray = generateprimes. primegenerator. generateprimenumbers ( 100 ); 36 Assert. areequal (centarray. length, 25 ); 37 Assert. areequal (centarray [ 24 ], 97 ); 38 } 39 40 41 42 43 }
Because nunit projects are used, we also need to add references to them.
In the nuget package console, enter PM> install-package nunit to add reference (very convenient! Remember to modify "Default project ");
Regenerate the solution, select a project in visual nunit, and click Run to view the running result.
Transfer solution to monodevelop
Copy the solution to the Linux operating system (I am using the Ubuntu release) and open the project in monodevelop.
First, check whether nunitrunner is installed in monodevelop. Select "about"-"" version information "from the" help "menu ".
If you do not need to go to the Ubuntu Software Center, select the monodevelop additional component and update the component.
Select a unit test project and change active layout in the toolbar to unit testing.
Right-click the unit test project and select Run item to view the test result.
the above is just a simple introduction of a unit test in visual, of course, there are many other methods to achieve the purpose.