Friday, 6 March 2015

C# - Nunit Script Creation and Execution

- Visual Studio is installed, click link for 'Visual Studio' installation .
- Nunit is installed, click link for 'NUnit GUI Runner' installation.

1. Open the Visual Studio

2. Select the Class Library.
   Enter Name and Solution Name.
   Click 'OK' button.

3. Class is created.

4. Expand 'References' in 'Solution Explorer'.

5. Mouse right click on 'References', select 'Add Reference'.

6. Select the 'Browse' tab, navigate to the folder where NUnit dll's are present.
    Select the dll's and click 'ok' button. 
          ex:C:\Program Files (x86)\NUnit 2.6.4\bin\framework

7. 'nunit.framework' is added in the 'References'.

8. Add Nunit attributes to the class, method and some assertion code to perform validation.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace NunitTest1
    public class Class1
        public void Test1()
            int actualResults = 30;
            int expectedResults = 30;

            Assert.AreEqual(expectedResults, actualResults);


9. Build the solution.

10. Open the 'Nunit-GUI Runner' installed in your machine.

11. Select 'File' --> 'Open Project' in Nunit GUI Runner.

12. Select the project dll which is created at step 9 and click on 'Open' button.

13.  All the classes and methods marked by NUnit attributes are shown in the NUnit GUI Runner.

14. Select the method and click 'Run' button.
15. You can see the result as PASSED.

16. Now lets verify the failed results.
      Go to the code modify the Actual Results 30 to 40.

17. Rebuild the solution.

18. Go to Nunit GUI runner, now click on the 'Run' button again.

19. You can see the results as failed, since actualResults(40) and expectedResults(30) doesn't match.

No comments:

Post a Comment