ASP.NET开源MVC框架VICI 测试的便利性

       平滑的学习曲线是微软技术的最大特征,在ASP.NET上,从一开始我们什么都不会,到慢慢的基本什么都会了,下一个值得向我这样的菜鸟突破的是什么呢?是海量并发?海量数据?高可扩展性?高度安全性?高度稳定性?

      开始我觉得可能可扩展性比较重要,经过一年满负荷的工作,我现反倒觉得他们都是在扯淡,除了给脸上贴金之外没什么用,应用程序的可测试性应该是学下一道门槛,不可能测试的项目会令项目组进入开发组开发-》测试组手工测试出现BUG=》开发组开发改bug恶性的循环,最要命的是不可测试的程序无法让测试组 使用绝大多数测试工具,造成bug潜伏;

  另外,可测试性也是个人学习 《重构》、《单元测试》、《性能优化》等的前提,自己写的东西不可测试,这些东西看了一点用也没用,书中每100个字就要4个单词是测试- -;这些东西都需要用测试来检验所做的修改是否正确。可测试性应该是ASP.NET学习的第二道门坎(PS 第一道门坎是 ViewState

以前关于vici的文章 http://www.cnblogs.com/qqloving/category/292931.html

开源ASP.NET mvc (官网地址:http://viciproject.com/index

有人可能说 微软搞那个开源的mvc已经很不错了,可是在可测试方面和vici比起来 还差的很远

官网的案例下载包含测试代码:http://viciproject.com/wiki/Projects/Mvc/Download

vicimvc 为以下几个方面提供了非常简单的测试方法

1 没有登录的用户进入授权页面时,页面应跳转到登陆页面

2 检查应用程序是否正确重定向到登录页

3 填写用户名和密码 登录(模拟登录)

4 检查当前页面是否包含我们所需要的html标签

测试部分的代码

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

using Vici.Mvc;

using NUnit.Framework;
using Demo.ViciMvc;


namespace UnitTest.Demo.ViciMVC
{
    /// <summary>
    /// Summary description for UnitTest2
    /// </summary>
    [TestFixture]
    public class UnitTest2
    {
        /// <summary>
        /// 测试没有登录的用户访问main 页面 则需要跳转到login登陆页
        /// 
        /// </summary>
        [Test]
        public void TestUnauthorizedAccess()
        {
            OfflineWebSession webSession = CreateWebSession();

            // Navigate to a page which requires login
            webSession.PageGet("/main");

            // Check if we were redirected to the login page
            Assert.AreEqual("/login", webSession.CurrentPage);
        }

        /// <summary>
        /// 测试用户登录
        /// 什么编写测试脚本 录制测试脚本 都弱爆了!!!!
        /// </summary>
        [Test]
        public void TestLogin()
        {
            OfflineWebSession webSession = CreateWebSession();

            // Navigate to the login page
            webSession.PageGet("/login");

            // Enter login, password and click on the "btnLogin" button
            webSession.PostData["login"] = "demo";
            webSession.PostData["password"] = "promesh";
            webSession.PushButton("btnLogin");

            // Post form
            string html = string.Empty;
           html= webSession.PagePost(webSession.CurrentPage);

            // Check if we were redirected to the main page
            Assert.AreEqual("/main", webSession.CurrentPage,html);

            // Load the user we logged in as
            User user = DataService.FindUser("demo", "promesh");

            // Check if the session contains the correct user
            Assert.AreEqual(Application.Session.User.UserID, user.UserID, "Wrong user logged in???");
        }

        private string Login(OfflineWebSession webSession)
        {
            webSession.PageGet("/login");

            // Enter login, password and click on the "btnLogin" button
            webSession.PostData["login"] = "demo";
            webSession.PostData["password"] = "promesh";
            webSession.PushButton("btnLogin");
           return  webSession.PagePost(webSession.CurrentPage);

        

            // Post form
            //return webSession.CurrentPage;

        }

        [Test]
        public void TestEditPage()
        {
            OfflineWebSession webSession = CreateWebSession();

            //这个是当前页面的html 
            string html = Login(webSession);
            Assert.IsTrue(html.Contains("/employee/edit/1"), html);
   
        }

        private static OfflineWebSession CreateWebSession()
        {
            OfflineWebSession webSession = new OfflineWebSession(System.IO.Path.GetFullPath(@"..\..\..\Demo.ViciMVC"));

            webSession.FollowRedirects = true;

            return webSession;
        }
    }
}

 

vici 能够进行有效的测试 是他对session进行了自己特殊的实现 自己实现了一个OfflineWebSession, 专门用来做测试

 

CreateWebSession 用来创建一个测试用的session,

 

        private static OfflineWebSession CreateWebSession()
        {
            OfflineWebSession webSession = new OfflineWebSession(System.IO.Path.GetFullPath(@"..\..\..\Demo.ViciMVC"));

            webSession.FollowRedirects = true;

            return webSession;
        }

 

vici 的测试代码有一些配置项

 

<configuration>

  <appSettings>
    <add key="Mvc.ApplicationClass" value="Demo.ViciMvc.Application, Demo.ViciMVC" /> 框架的dll
    <add key="Mvc.TemplatePath" value="Content/templates" /> 模板的路径
  </appSettings>

</configuration>

@"..\..\..\Demo.ViciMVC" 为要测试项目的地址

 

一提到要登录,就要输入用户名呢密码或者录制测试脚本,vici的实现超级简单

 

 /// <summary>
        /// 测试用户登录
        /// 什么编写测试脚本 录制测试脚本 都弱爆了!!!!
        /// </summary>
        [Test]
        public void TestLogin()
        {
            OfflineWebSession webSession = CreateWebSession();

            // Navigate to the login page
            webSession.PageGet("/login");

            // Enter login, password and click on the "btnLogin" button
            webSession.PostData["login"] = "demo";
            webSession.PostData["password"] = "promesh";
            webSession.PushButton("btnLogin");

            // Post form
            string html = string.Empty;
           html= webSession.PagePost(webSession.CurrentPage);

            // Check if we were redirected to the main page
            Assert.AreEqual("/main", webSession.CurrentPage,html);

            // Load the user we logged in as
            User user = DataService.FindUser("demo", "promesh");

            // Check if the session contains the correct user
            Assert.AreEqual(Application.Session.User.UserID, user.UserID, "Wrong user logged in???");
        }

 

webSession.PageGet("/login"); 和webSession.PagePost(webSession.CurrentPage); 返回一个字符串是当前请求的url 的页面的html,可以通过这个字符串是否包含我们需要的数据来 进行一些测试

例如 实例代码

        [Test]
        public void TestEditPage()
        {
            OfflineWebSession webSession = CreateWebSession();

   
            string html = Login(webSession);
            Assert.IsTrue(html.Contains("/employee/edit/1"), html);
   
        }

 

vici 的测试代码 无论是 通过

NUnit.Framework 还是 微软自带的测试框架 都可以测试的

所以 vic 果断 NX

 

 

 

 

 

 

 

 

 

 

posted @ 2012-01-14 16:12  互联网Fans  阅读(2317)  评论(0编辑  收藏  举报