zoukankan      html  css  js  c++  java
  • 基于模型生成自动化测试用例

    在前面一文使用NModel自动生成测试用例中,介绍了如何通过给待测试的程序建模,生成测试用例的方法。但前面文章的问题是,生成的都是手工的测试用例,如果让测试人员手工执行程序自动生成的测试用例,呃……这对于测试工程师来说,不蒂于一场噩梦。

    自动产生的测试用例本就应该由程序自动执行,这其实也就是NModel推荐的模式。先回过头来看看文章中制作的模型,模型里面将登录、注销、用户名以及密码等要素都抽象出来了,而NModel是以这些抽象出来的动作(登录、注销)和状态(用户名、密码)为依据,产生测试用例的。那么要让NModel自动执行产生的测试用例,那么它需要知道如何才能执行登录和注销这些动作,而且它还需要知道在执行登录操作时,User.Administrator这个抽象出来的用户在实际执行测试用例时,应该采用什么用户名等信息。而这些信息NModel自己是无法“猜测”出来的,需要测试人员的引导才能获得。

    因此NModel提供了IStepper这个接口让测试人员提供所需的信息,下面是这个接口的完整定义:

    namespace NModel.Conformance

    {

        // Summary:

        //     Must be implemented by an IUT for conformance testing

        public interface IStepper

        {

            // Summary:

            //     Make a step according to the given action, the current state becomes the

            //     target state of this transition. If the action is not enabled an exception

            //     is thrown and the resulting state is undefined. An action on null may be

            //     returned.

            //

            // Parameters:

            //   action:

            CompoundTerm DoAction(CompoundTerm action);

            //

            // Summary:

            //     Return to the initial state. If Reset is not enabled in the current state,

            //     an exception is thrown and the resulting state is undefined and is thus not

            //     guaranteed to be the initial state

            void Reset();

        }

    }

     

    这个接口的定义非常简单(但是实现起来就是另外一回事了),核心的函数是 DoAction,从名字可以看出,这个函数就是让测试人员自己实现前面模型中每一个动作。而NModel的工作就是将这些动作按照模型定义好的序列组合多个基本的动作。这样多个基本动作的组合就模拟了现实生活中,用户使用产品时的用户场景。在我们这个例子中,要实现的动作就是所有加上[Action]属性的函数,即Login_StartLogoutWebSiteModel类里面的Initialize

    我们看InitializeLogin_Start动作的实现:

    private void Initialize()

    {

        TestSettings = TestLibrary.SetupTest();

    }

     

    public CompoundTerm DoAction(CompoundTerm action)

    {

        switch (action.Name)

        {

            case "Initialize":

                Initialize();

                break;

     

            case "Login_Start":

                string userName;

                modelUserToRealUser.TryGetValue((string)

                    ((CompoundTerm)action.Arguments[0])[0], out userName);

     

                string userPass = string.Empty;

                switch ((string)((CompoundTerm)action.Arguments[1])[0])

                {

                    case "Correct":

                        userPass = realUserPassword[userName];

                        break;

     

                    case "Incorrect":

                        userPass = wrongPassword;

                        break;

                }

     

                TestSettings.UserHelper.LogOn(userName, userPass);

                var status = LoginStatus.Success;

                if (TestSettings.UserHelper.IsLoginFaled())

                    status = LoginStatus.Failure;

     

                break;

        }

     

        return null;

    }

     

    这里,我用的是Selenium来实现网站测试用例的自动化, Initialize这个动作很简单,就是在执行所有自动化测试用例之前,做一些初始化操作,在我这个例子当中,就是启动浏览器,将测试用例连接到Selenium-RC上。读者如果不了解Selenium以及网站自动化测试的话,可以参考我的这篇文章网站测试自动化系统—基于SeleniumVSTT ,实际上Initialize函数里面的TestLibrary.SetupTest也是从那篇文章里拷贝出来的。

    Login_Start这个函数就有点复杂了,因为在模型里面,Login_Start这个动作要求两个参数,或者说是状态用户名和密码。而且用户分为AdministratorAuthenticated两类,密码也类似。那么在哪个地方将这些抽象的状态具体化呢?当然是在执行测试用例之前,要么是构造函数,要么是前文说到的 Initialize函数。我的例子里面是在构造函数里执行初始化操作:

    public string wrongPassword;

    public Dictionary<ModelUser, RealUser> modelUserToRealUser;

    public Dictionary<RealUser, string> realUserPassword;

     

    public Site()

    {

        modelUserToRealUser = new Dictionary<string, string>();

        modelUserToRealUser.Add("Anonymous", TestLibrary.Consts.ContributorUser);

        modelUserToRealUser.Add("Authenticated", TestLibrary.Consts.ModeratorUser);

        modelUserToRealUser.Add("Author", TestLibrary.Consts.AuthorUser);

        modelUserToRealUser.Add("Administrator", TestLibrary.Consts.AdminUserName);

     

        realUserPassword = new Dictionary<string, string>();

        realUserPassword.Add(TestLibrary.Consts.AdminUserName, TestLibrary.Consts.CommonPassword);

        realUserPassword.Add(TestLibrary.Consts.AuthorUser, TestLibrary.Consts.CommonPassword);

        realUserPassword.Add(TestLibrary.Consts.ContributorUser, TestLibrary.Consts.CommonPassword);

        realUserPassword.Add(TestLibrary.Consts.ModeratorUser, TestLibrary.Consts.CommonPassword);

     

        wrongPassword = "abcefghi";

    }

     

    将所有抽象出来的动作和状态具体化以后,就可以让 NModel产生测试用例并根据我们提供的信息自动执行测试用例了,下面这个命令完成这个操作:

    Ct.exe /r:TrainStep.dll /r:TrainMode.dll /iut:Train.Test.Site.Create TrainMode.WebSiteModel.CreateLoginModel

    Ct.exe用来产生并执行测试用例,为了完成这个工作,ct.exe需要知道模型(TrainModel.dll中的TrainModel.WebSiteModel.CreateLoginModel)和模型的一个实例(TrainStep.dll),但是/iut这个参数是干什么用的?因为一个dll文件当中,你可以有模型的多个实例,或者不同模型的实例,因此,ct.exe需要你明确指定创建模型实例的方法(Train.Test.Site.Create)。

    本文所基于的产品是:http://orchard.codeplex.com/ 。感兴趣的读者,可以自己把产品源代码下载编译好,并用下面的测试模型和自动化代码尝试一下模型驱动测试的威力:

    /Files/killmyday/Orchard.NModel.Test.zip

  • 相关阅读:
    JAVA 正则表达式 (超详细)
    <select>改造成<s:select>实现表单的回显功能
    打开新界面
    list删除操作 java.util.ConcurrentModificationException
    C# 增加 删除 更新 方法
    C# 网页内容获取
    excel 处理方法
    C# 读取excel
    sql 导入excel 遇到问题
    DataSet
  • 原文地址:https://www.cnblogs.com/killmyday/p/1726505.html
Copyright © 2011-2022 走看看