Simplified C # version of JUnit

Source: Internet
Author: User
Preface

This is a simple wheel I made to put it on the MACOs.

Today, I checked some implementations of JUnit 3.8 and decided to use C # to imitate the famous JUnit framework in Java.

Implementation requirements:

For some user-defined classes, run the public, void, and non-parameter methods.

Implementation:

Testcase

User-oriented interface, which contains a public test () method. This method calls its specified instance method (unit test method ).

 Public   Abstract   Class  Testcase {  Public   String Testmethodname { Get ; Set  ;} Public   Void  Test (){  This  . Basetest ();}  ///   <Summary>          ///  The instance object of each testcase will run the method specified according to testmethodname.  ///  With this design, each test method corresponds to an instance. If there are multiple test methods in a class, each test method does not interfere with each other on an instance object.  ///  If only one instance is used, other methods of the class cannot be tested because an exception is thrown by a method of the class.  ///  </Summary>          Protected   Void  Basetest () {methodinfo Method = This . GetType (). getmethod (testmethodname, New  Type [] {});  This  . Beforetest ();  Try  {Method. Invoke (  This , Null ); Console. writeline (  "  Method Test successful  "  );}  Catch  (Exception ex) {console. writeline (  "  Failed! {0}  "  , Ex. Message );}  Finally  {  This . Aftertest ();}}  Public   Abstract   Void  Beforetest ();  Public   Abstract   Void  Aftertest ();} 

 

 

 

 

Testrunner

Test operator,

This class has one

Public void addtestcase (type test)

The method is provided to the client to specify the classes to be tested by the user.

 

 Public   Class  Testruner {  Public  Testruner () {testcases = New List <type> (); Tests = New List <testcase> ();}  Private List <type> Testcases;  Private List <testcase>Tests;  Public   Long Milles { Get ; Private   Set  ;}  Public   Void  Addtestcase (type test) {testcases. Add (TEST );}  Public   Void  Run (){  This . Baserun ();}  Private   Void  Baserun () {buildtest (); stopwatch Timer = New  Stopwatch (); timer. Start ();  Foreach ( VaR Item In  Tests) {item. Test ();} timer. Stop ();  This . Milles =Timer. elapsedmilliseconds ;}  ///   <Summary>          ///  Build a test and scan all methods of the specified type to be tested,  ///   </Summary>          Private   Void  Buildtest (){  //  Testcases is type              Foreach ( VaR Item In Testcases ){  Foreach ( VaR Method In  Item. getmethods () {addtestmethod (method, item );}}}  Private   Void  Addtestmethod (methodinfo method, type testclass ){  If  (Istestmethod (method) {addtest (createtest (method, testclass ));}}  Private  Bool  Istestmethod (methodinfo method) {parameterinfo [] parames = Method. getparameters (); Type returntype = Method. returntype;  Return Method. Name. startswith ( "  Test  " ) & Parames. Length = 0 & Returntype = Typeof ( Void  );} ///   <Summary>          ///  If a test method that meets the requirements is found, create a testcase Instance Object and specify the method name of the test method to be called for this object.  ///   </Summary>          ///   <Param name = "method"> </param>          ///   <Param name = "testclass"> </param>          ///   <Returns> </returns>          Private Testcase createtest (methodinfo method, type testclass) {constructorinfo = Testclass. getconstructor ( New  Type [] {}); testcase = Constructorinfo. Invoke ( Null ) As  Testcase; testcase. testmethodname = Method. Name;  Return  Testcase ;}  Private   Void Addtest (testcase test) {tests. Add (TEST );}} 

 

 

 

 

The above two classes can be found:

The class in testrunner uses reflection to call the method to be tested (in fact, the actual call to the unit test method is handed over to the testcase Instance Object for self-execution. Testrunner is only responsible for developing methods ).

The testcase class undertakes: it provides interfaces for the user's unit test and runs the specific unit test (testbase uses the adapter mode adapter.BecauseTestrunnerUnable to know the Unit Test Written by the userThe method name. Therefore, the adapter mode is required. Use the testbase method in testcase for a transfer, and switch the interface to test to provideTestrunnerCall).

 

Client:

 Static   Void Main ( String [] ARGs) {testruner run = New  Testruner (); run. addtestcase (  Typeof  (Classdafgsadfsafqqwwfqs2); run. addtestcase (  Typeof  (Class1); console. writeline (  "  Start testing  "  ); Run. Run (); console. writeline (  "  Test ended. Time consumed: {0}  " , Run. Milles); console. Read ();} 

 

 

Extended task:

1. Use attribute tags to meet the above requirements

2. Use Java to re-simulate

3. Complete JUnit Framework ResearchCode(Review its code framework)

4. view the nunit design.

 

 

Reference link:

Https://github.com/KentBeck/junit

 

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.