模型生成自动化测试用例

自动产生的测试用例本就应该由程序自动执行,这其实也就是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.Arguments0)0, out userName); string userPass = string.Empty; switch ((string)((CompoundTerm)action.Arguments1)0) { case "Correct": userPass = realUserPassworduserName; 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<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)。

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

相关推荐
大树8811 小时前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠11 小时前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质11 小时前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
小宇宙Zz12 小时前
Maven依赖冲突
java·服务器·maven
Inhand陈工12 小时前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
酣大智13 小时前
ARP代理--工作原理
运维·网络·arp·arp代理
shushangyun_13 小时前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
古城小栈13 小时前
Unix 与 Linux 异同小叙
linux·服务器·unix
施努卡机器视觉14 小时前
SNK施努卡侧滑门锁上滑轮总成自动化装配线,从零件到组件,全流程精密制造方案
运维·自动化·制造
程序猿阿伟14 小时前
《Chrome离线扩展安装的底层逻辑与场景落地指南》
服务器·网络·chrome