[C #] _ [unit test of business logic code using NUnit]

Source: Internet
Author: User

Nunit is a testing framework used in the xUnit series for C #, such as JUnit and Gtest. You can perform unit tests on the business logic of classes and interfaces. Currently, developers who do not perform unit tests are too unprofessional. Scenario: 1. when writing website code, I wrote an independent Query Class Library. But how can I test the logic of this class library? the slow way is to manually click the mouse and perform a test on the webpage. However, this efficiency is too low, especially when a problem occurs, you have to constantly modify the code-run-click the page. It is a waste of time for developers to repeat such iterations, especially when some logic is modified, and the code is quite unreliable. 2. If there is a unit test, you can use the prepared test cases for regression testing. For example, if you write test data for some boundary conditions, you only need to re-run the unit test after modifying the code. 3. There are enough benefits .. If you are familiar with it, the efficiency is not improved by half past one. If you think the time is precious, we suggest using unit testing. 4. Although VS2010 also comes with Microsoft's testing framework, I do not recommend it, because these are too coupled to the IDE, and the entire test case may not be used with IDE. Step 1: Download The Nunit http://www.nunit.org/and download the zip package. We recommend that you use the green zips to verify the Registration Form of computers that do not need to be contaminated. Decompress it and place it in: E: \ software \ NUnit-2.6.2 \ put E: \ software \ NUnit-2.6.2 \ binning to the user's environmental variation path, this can directly run the executable file. nunit-lele.exe through the command line to run the benefit is can join continuous integration, that is, automated testing, the code is continuously built and tested, and the exe can be directly called through ide. step 2: Create the class library QX_Users. compile the class library file QueryHelper. cs. I have changed it here. net frameworkd 2.0, generate the QX_Users.dll library. [Csharp] view plaincopyusing System; using System. collections. generic; using System. text; namespace QxsLogic {public class QueryHelper {public List <String> QueryUsers (String userId) {String SQL = "select name from users"; Console. writeLine (SQL); List <String> lists = new List <string> (); lists. add ("infoworld"); lists. add ("hero"); lists. add (userId); return lists ;}} Step 3: Create a new tests directory under the QX_Users directory. To directly facilitate maintenance of the test code and project code, we recommend that you use this project structure and add a tests directory. step 4: Create the Test_QX_Users project and test code TestQueryHelper. cs. We recommend that you use this command to intuitively know which class to test. Add a reference to NUnit. framework. dll under the nunit directory directly by browsing. Generate the Test_QX_Users.dll library. [Csharp] using System; using System. collections. generic; using System. text; using QxsLogic; namespace Test_QX_Users {using NUnit. framework; [TestFixture] public class TestQueryHelper {[Test] public void TestQueryUsers () {QueryHelper query = new QueryHelper (); String id = "hero "; list <String> lists = query. queryUsers (id); foreach (String name in lists) {Console. writeLine (name);} Assert. greater (Lists. count, 0); Assert. areEqual (id, lists [lists. count-1]); Console. writeLine ("finish Test") ;}} Step 5: Create a new batch under the Test_QX_Users project directory (this location can not pollute the Test code), name the nunit-test.bat, it is recommended to use this name, you can know which test framework to use: [plain] nunit-console Test_QX_Users \ bin \ Debug \ Test_QX_Users.dll Step 6: run the batch processing nunit-test.bat, Nunit outputs a TestResult in the current directory by default. the xml test report can be used in continuous integration. [Plain] C: \ Users \ apple \ Documents \ Visual Studio 2010 \ Projects \ ClassLibrary1 \ ClassLibrary1 \ tests \ Test_QX_Users> nunit-test.bat C: \ Users \ apple \ Documents \ Visual Studio 2010 \ Projects \ ClassLibrary1 \ ClassLibrary1 \ tests \ Test_QX_Users> nunit-console Test_QX_Users \ bin \ Debug \ release NUnit-Console version 2.6.2.12296 Copyright (C) 2002-2012 Charlie Poole. copyright (C) 2002-2004 James W. newkirk, Michael C. two, Alexei. vorontsov. copyright (C) 2000-2002 Philip Craig. all Rights Reserved. runtime Environment-OS Version: Microsoft Windows NT 6.1.7601 Service Pack 1 CLR Version: 2.0.50727.5466 (Net 3.5) ProcessModel: Default DomainUsage: Single Execution Runtime: net-3.5. select name from users infoworld hero finish Test Tests run: 1, Errors: 0, Failures: 0, Inconclusive: 0, Time: 0.1430082 seconds Not run: 0, Invalid: 0, ignored: 0, Skipped: 0

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.