在vs2010,vs2013上弄了二三天,没能在asp.net上弄通微软的单元测试。不得已,自己打造一个单元测试框架,仅仅花费不到一个小时就完事了。
这个框架完全是Web代码中的一部分,在相同的進程空间中运行,因此不需要弄什么专门的访问器(微软这玩意根本就是个废物,不单复杂还不顶用,连我的一个简单的登录函数,见下,都测试不了),可直接引用网站内的任何对象、方法,使用既简单又方便,编写测试用例时比任何流行的框架都来得容易。
创建测试用例的过程如下:
- 首先,将本测试框架代码(仅一个类,assert.cs)复制到网站的App_Code中。
- 编写自己的单元测试类DBIntfTest,用来对数据访问层的类DBIntf作单元测试。代码如下:
using System;
using System.Data;
using System.Collections.Generic;
using pclion.IDB;
using pclion.UnitTest;
using ftmark;
namespace dbintfTest
{
/// <summary>
///这是 DBIntfTest 的测试类,旨在对类 ftmark.DBIntf 作单元测试
///</summary>
public class DBIntfTest
{
const string connstr = "Data Source=.;Persist Security Info=True;User id=sa;Password=123456;Initial Catalog=exam2013";
//private TestContext testContextInstance;
public int examid=8;
public string usercode = "yw0707";
public string passwd = "yw0707";
public int userid = 39;
public DBIntf dbintf;
public Config config;
public DBIntfTest()
{
dbintf = new DBIntf();
dbintf.Open(connstr);
config = new Config();