InterviewSolution
| 1. |
How Can We Make Nunit Test Cases Data Driven? |
|
Answer» In the previous questions we had hardcoded the test data in the NUNIT test case itself. But in real time you would like to have test data inputs coming from a XML file or a database, in other words you would like to create data driven test cases. In order to create data driven in NUNIT we can attribute the UNIT test method with ‘TestCaseSource’ attribute as shown in the below figure. In ‘TestCaseSource’ we need to provide the function which will return the test case data, in this case it is ‘TestCases’. Below is the code snippet which provides DYNAMIC data to unit test method. The function which is providing the dynamic data should return ‘IEnumerable’. NUNIT provides something called as ‘TestCaseData’ class. This class defines the test case data for NUNIT. The class provides ways by which you can specify input and the expected OUTPUT from the test case. Finally to return test case by test case we need to use ‘yield’ keyword with for each. You can SEE the below code snippet to understand how yield works. In the previous questions we had hardcoded the test data in the NUNIT test case itself. But in real time you would like to have test data inputs coming from a XML file or a database, in other words you would like to create data driven test cases. In order to create data driven in NUNIT we can attribute the unit test method with ‘TestCaseSource’ attribute as shown in the below figure. In ‘TestCaseSource’ we need to provide the function which will return the test case data, in this case it is ‘TestCases’. Below is the code snippet which provides dynamic data to unit test method. The function which is providing the dynamic data should return ‘IEnumerable’. NUNIT provides something called as ‘TestCaseData’ class. This class defines the test case data for NUNIT. The class provides ways by which you can specify input and the expected output from the test case. Finally to return test case by test case we need to use ‘yield’ keyword with for each. You can see the below code snippet to understand how yield works. |
|