C#版本的xUnit的测试框架模型和xUnit.NET开源项目

1. 参考kent beck的测试驱动写的C#测试框架模型

    a) 测试用例: WasRun, 基类为TestCase

    b) 框架: TestCaseTest用来测试TestCase,本身也是它的子类,可使用run调用自己的方法

    c)重点:为WasRun和TestCase,添加setUp,tearDown功能

    d)然后,TestResult用来返回和传递结果,主要是计数的结果

     e) 最后,TestSuite封装一组测试,并在TestCaseTest中测试

      WasRun<=>TestCase<=>TestSuite<=>TestCaseTest<=> Main

 

2.另外开源完整的测试框架可参考这里 https://github.com/xunit/xunit

 

贴上模型的代码,大家自己按类区分吧。

namespace xUnit
{
    class Program
    {
        static void Main(string[] args)
        {
            //(new TestCaseTest("testTemplateMethod")).run();

            //(new TestCaseTest("testTemplateMethod")).run();
            //(new TestCaseTest("testResult")).run();
            //(new TestCaseTest("testFailedResultFormatting")).run();
            //(new TestCaseTest("testFailedResult")).run();

            (new TestCaseTest("testSuite")).run(new TestResult());

            Console.ReadLine();
        }
    }
}

namespace xUnit
{
    class TestResult
    {
        private int runCount;
        private int errorCount;
        public TestResult()
        {
            runCount = 0;
            errorCount = 0;
        }
        public string summary()
        {
            string sumStr = "" + runCount + " run, " + errorCount + " failed";
            return sumStr;
        }
        public void testStarted()
        {
            runCount++;
        }
        public void testFailed()
        {
            errorCount++;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace xUnit
{
    class TestCaseTest : TestCase
    {
        private string p;

        public TestCaseTest(string p) : base(p)
        {
            // TODO: Complete member initialization
            this.p = p;
        }

       //private WasRun test;
        TestResult result;
        public override void setUp()
        {
            //test = new WasRun("testMethod");
            result = new TestResult();
        }

        public void testTemplateMethod()
        {
            WasRun test = new WasRun("testMethod");
            test.run(result);
           
            try
            {
                Assert.AreEqual("setUp testMethod tearDown ", test.log);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public void testResult()
        {
            WasRun test = new WasRun("testMethod");
            test.run(result);
            try
            {
                Assert.AreEqual("1 run, 0 failed", result.summary());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public void testFailedResult()
        {
            WasRun testFailed = new WasRun("testBrokenMethod");
            testFailed.run(result);
            try
            {
                Assert.AreEqual("1 run, 1 failed", result.summary());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public void testFailedResultFormatting()
        {
            result.testStarted();
            result.testFailed();
            try
            {
                Assert.AreEqual("1 run, 1 failed", result.summary());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public void testSuite()
        {
            TestSuite suite = new TestSuite();
            suite.add(new WasRun("testMethod"));
            suite.add(new WasRun("testBrokenMethod"));
            suite.run(result);
            try
            {
                string msg = result.summary();
                Console.WriteLine(msg);
                Assert.AreEqual("2 run, 1 failed", msg);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    
    }


}

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace xUnit
{
    class TestSuite
    {
        ArrayList tests;
        public TestSuite()
        {
            tests = new ArrayList();
        }
        public void add(WasRun test)
        {
            tests.Add(test);
        }
        public void run(TestResult result)
        {
            foreach (WasRun test in tests)
            {
                test.run(result);
            }
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace xUnit
{
    class TestCase
    {
        protected string methodName = "";
        public TestCase()
        {
        }

        public TestCase(string methodName)
        {
            this.methodName = methodName;
        }

        public void run(TestResult result)
        {
            result.testStarted();

            setUp();
            try
            {
                Type t = this.GetType();
                MethodInfo method = t.GetMethod(methodName);
                method.Invoke(this, null);
            }
            catch (Exception e)
            {
                result.testFailed();
                Console.WriteLine("运行测试程序设定抛出的异常," + e.InnerException.Message);
            }
          
            tearDown();
        }

        public virtual void setUp()
        {
        }
        public virtual void tearDown()
        {
        }
     
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace xUnit
{
    class WasRun : TestCase
    {
        private string p;
        public string log = "";

        public WasRun(string p)
            : base(p)
        {
            // TODO: Complete member initialization
            this.p = p;
        }

        public void testMethod()
        {
            //wasRun = true;
            log += "testMethod ";
        }

        public void testBrokenMethod()
        {
            throw new Exception("testBrokenMethod test failed");
        }

        public override void setUp()
        {
            //wasRun = false;
            //wasSetUp = true;
            log += "setUp ";
        }
        public override void tearDown()
        {
            log += "tearDown ";
        }

    }
}

 

posted on 2014-04-21 10:48  Wolf Li  阅读(1819)  评论(0编辑  收藏  举报

导航