• 模型生成自动化测试用例


    自动产生的测试用例本就应该由程序自动执行,这其实也就是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_Start,Logout和WebSiteModel类里面的Initialize。

    我们看Initialize和Login_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以及网站自动化测试的话,实际上Initialize函数里面的TestLibrary.SetupTest也是从那篇文章里拷贝出来的。

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

    public string wrongPassword;

    public Dictionary modelUserToRealUser;

    public Dictionary realUserPassword;

    public Site()

    {

        modelUserToRealUser = new Dictionary();

        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();

        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)。

    【整整200集】超超超详细的Python接口自动化测试进阶教程合集,真实模拟企业项目实战

  • 相关阅读:
    Pyecharts绘图笔记
    Java期末大作业补救基地~ 谁还不会点JDBC?【Java Swing 和 Mysql】历时三天三夜的代码&&直接白拿
    python进程、线程
    【SSL集训DAY1】C【暴力】【数学】
    Android——解决BottomNavigationView+Fragment重建与重叠问题
    变周期控制思路
    MySQL FLOAT、DOUBLE、DECIMAL(小数类型)
    OpenResty“更好更灵活的 Nginx”,这份OpenResty开发指南你值得拥有
    一文拿下HTTP
    【软件测试】作为测试人,因工作与开发吵了一架碰撞,该咋办......
  • 原文地址:https://blog.csdn.net/ada4656/article/details/132740777