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 ";
}
}
}
C#版本的xUnit的测试框架模型和xUnit.NET开源项目的更多相关文章
- React MVC框架 <某某后台商品管理开源项目> 完成项目总结
**百货后台商品信息开源项目 1.利用React app脚手架 2.封装打包 buid 3.更偏向于后台程序员开发思维 4.利用的 react -redux react-router-dom ...
- 使用 xunit 编写测试代码
使用 xunit 编写测试代码 Intro xunit 是 .NET 里使用非常广泛的一个测试框架,有很多测试项目都是在使用 xunit 作为测试框架,不仅仅有很多开源项目在使用,很多微软的项目也在使 ...
- Selenium 4 Python的最佳测试框架
随着Python语言的使用越来越流行,基于Python的测试自动化框架也越来越流行.在项目选择最佳框架时,开发人员和测试人员会有些无法下手.做出选择是应该判断很多事情,框架的脚本质量,测试用例的简单性 ...
- 消灭Bug!十款免费移动应用测试框架推荐
对于移动应用开发者而言,Bug往往是最让人头疼的一大问题.不同于时时刻刻可以修补的Web App,移动App中的Bug往往隐藏得很深,甚至有时候等到用户使用才显现出来,这么一来开发者搞不好就会赔了 ...
- python pytest测试框架介绍二
在介绍一中简单介绍了pytest的安装和简单使用,接下来我们就要实际了解pytest了 一.pytest的用例发现规则 pytest可以在不同的函数.包中发现用例,发现的规则如下 文件名以test_开 ...
- React躬行记(14)——测试框架
测试不仅可以发现和预防问题,还能降低风险.减少企业损失.在React中,涌现了多种测试框架,本节会对其中的Jest和Enzyme做详细的讲解. 一.Jest Jest是由Facebook开源的一个测试 ...
- phpunit 测试框架安装
PHPUnit是一个轻量级的PHP测试框架.它是在PHP5下面对JUnit3系列版本的完整移植,是xUnit测试框架家族的一员(它们都基于模式先锋Kent Beck的设计).来自百度百科 一.下载wg ...
- Google C++测试框架系列:入门
Google C++测试框架系列:入门 原始链接:V1_6_Primer 注 GTest或者Google Test: Google的C++测试框架. Test Fixtures: 这个词实在找不到对应 ...
- 收藏清单: python测试框架最全资源汇总
xUnit frameworks 单元测试框架 frameworks 框架 unittest - python自带的单元测试库,开箱即用 unittest2 - 加强版的单元测试框架,适用于Pytho ...
随机推荐
- 分享一个基于ligerui的系统应用案例ligerRM V2(权限管理系统)(提供下载)
阅读目录 简介 系统特色 系统介绍 - 首页 系统介绍 - 列表页 系统介绍 - 明细页(表单) 系统介绍 - 菜单/按钮 系统介绍 - 权限中心 系统介绍 - 数据权限 系统介绍 - 字段权限 系统 ...
- 将思维转向rss
本屌丝因为穷住在了离市区比较远的农民房,平时上下班单程地铁时间接近一小时.在这漫长的一小时里,总得干点什么来蹉跎这段时光,看手机是最容易实现的事情.最地铁信号不好,手机也没什么好看的. 经过高人指点说 ...
- jQuery Recipies - 使用map来过滤对应的元素集
<table id="tblEmployee" class="normal_table"> <tr> <td>Employe ...
- Aeroo Reports Linux server
This article covers installation process for Aeroo reporting engine on Linux servers. If you find th ...
- Linux下SVN部署/安全及权限配置,实现web同步更新
转自:http://www.cnblogs.com/me115/archive/2013/04/07/3002058.html 本文包含以下内容: SVN服务器安装 SVN权限管理 SVN使用SASL ...
- 【DB2】NVL2函数
语法: NVL2(表达式1,表达式2,表达式3) 如果表达式1为空,返回值为表达式3的值.如果表达式1不为空,返回值为表达式2的值. 例子: SELECT ID, NVL2(SEX,'非空','空值' ...
- php引用(&)变量引用,函数引用,对象引用和参数引用用法
php引用(&)变量引用,函数引用,对象引用和参数引用用法 php的引用(就是在变量或者函数.对象等前面加上&符号) 在PHP 中引用的意思是:不同的名字访问同一个变量内容.与C语 ...
- nodejs express route 的用法
express 中文社区:http://expressjs.jser.us/community.html nodejs express route 的用法 1. 首先是最基本的用法. 1 2 3 4 ...
- 已知的问题:本文总结了与Telerik UI for ASP.NET Core相关的所有已知问题。
ASP.NET Core Framework 不支持数据表. 有关此限制的更多信息,请参阅dotnet / corefx#1039. 不支持本地化资源. 有关此限制的更多信息,请参阅dotnet / ...
- base、this、new、override、abstract、virtual、static
前言 本文主要来讲解一下C#中,自己觉得掌握的不怎么样或者用的不多,不太熟悉的关键字,主要包括base.this.new.override.abstract.virtual以及针对static字段和s ...