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 ";
        }

}
}

C#版本的xUnit的测试框架模型和xUnit.NET开源项目的更多相关文章

  1. React MVC框架 <某某后台商品管理开源项目> 完成项目总结

    **百货后台商品信息开源项目 1.利用React  app脚手架 2.封装打包 buid 3.更偏向于后台程序员开发思维 4.利用的 react -redux    react-router-dom  ...

  2. 使用 xunit 编写测试代码

    使用 xunit 编写测试代码 Intro xunit 是 .NET 里使用非常广泛的一个测试框架,有很多测试项目都是在使用 xunit 作为测试框架,不仅仅有很多开源项目在使用,很多微软的项目也在使 ...

  3. Selenium 4 Python的最佳测试框架

    随着Python语言的使用越来越流行,基于Python的测试自动化框架也越来越流行.在项目选择最佳框架时,开发人员和测试人员会有些无法下手.做出选择是应该判断很多事情,框架的脚本质量,测试用例的简单性 ...

  4. 消灭Bug!十款免费移动应用测试框架推荐

      对于移动应用开发者而言,Bug往往是最让人头疼的一大问题.不同于时时刻刻可以修补的Web App,移动App中的Bug往往隐藏得很深,甚至有时候等到用户使用才显现出来,这么一来开发者搞不好就会赔了 ...

  5. python pytest测试框架介绍二

    在介绍一中简单介绍了pytest的安装和简单使用,接下来我们就要实际了解pytest了 一.pytest的用例发现规则 pytest可以在不同的函数.包中发现用例,发现的规则如下 文件名以test_开 ...

  6. React躬行记(14)——测试框架

    测试不仅可以发现和预防问题,还能降低风险.减少企业损失.在React中,涌现了多种测试框架,本节会对其中的Jest和Enzyme做详细的讲解. 一.Jest Jest是由Facebook开源的一个测试 ...

  7. phpunit 测试框架安装

    PHPUnit是一个轻量级的PHP测试框架.它是在PHP5下面对JUnit3系列版本的完整移植,是xUnit测试框架家族的一员(它们都基于模式先锋Kent Beck的设计).来自百度百科 一.下载wg ...

  8. Google C++测试框架系列:入门

    Google C++测试框架系列:入门 原始链接:V1_6_Primer 注 GTest或者Google Test: Google的C++测试框架. Test Fixtures: 这个词实在找不到对应 ...

  9. 收藏清单: python测试框架最全资源汇总

    xUnit frameworks 单元测试框架 frameworks 框架 unittest - python自带的单元测试库,开箱即用 unittest2 - 加强版的单元测试框架,适用于Pytho ...

随机推荐

  1. HDU1505(HDU1506的加强版)

    昨天打 CF又跪了.近期睡不好睡不好睡不好-感觉整个人都累傻了,根本无办法写下去,只写了一题签到题就跪了orz..从未试过这么悲剧. 今天早上凭着我的意念("怨念").七点又起来了 ...

  2. Java小案例(行星移动)

    Java小案例 行星移动:参考:三百集 使用软件:idea2017,java 1,图片集:这里  (idea图片源放在target目录下,才能访问到),建议从小往上看... 2,定义MyFrame p ...

  3. 003java面试笔记——【java基础篇】从团八百失败面试总结的java面试题(未完待续)

    8.java 线程     1)线程概念,线程与进程      线程:线程是“进程”中某个单一顺序的控制流.也被称为轻量进程.线程是进程中的实体,一个进程可以拥有多个线程,一个线程必须有一个父进程.线 ...

  4. linux 的计划任务 cron

    https://serverfault.com/questions/587696/how-to-restart-php-fpm-from-cron 我也遇到了这个问题,想用cron 来启动php-fp ...

  5. Codeforces 112A-Petya and Strings(实现)

    A. Petya and Strings time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  6. Caused by: org.apache.ibatis.reflection.ReflectionException我碰到的情况,原因不唯一

    映射文件: <select id="selectKeyByUserId"  resultMap="Xxx">        <![CDATA[ ...

  7. automake--关于两个文件configure.in和Makefile.am的编写

    http://blog.csdn.net/shanzhizi/article/details/30251763 automake主要通过编辑Makefile.am来控制它的行为,下面就常用的三个Mak ...

  8. 机器学习基石第一讲:the learning problem

    博客已经迁移至Marcovaldo's blog (http://marcovaldong.github.io/) Andrew Ng的Machine Learning比較简单,已经看完.林田轩的机器 ...

  9. 下载论坛源码GBK UTF8 BIG5分别是什么意思

    下载论坛源码GBK UTF8 BIG5分别是什么意思? 提问者:ly1987520 | 浏览次数:4010次 下载论坛源码简体中文GBK 简体中文UTF8 繁体中文BIG5 分别是什么意思?他们的区别 ...

  10. CAD启动找不到AC1ST16.DLL

    今天在安装Win7 x64上CAD2006启动报错:找不到ac1st16.dll文件. 一查,是系统变量的问题.在系统变量Path中cad的路径为: C:\Program Files (x86)\Co ...