基于模型生成自动化测试用例

发表于:2018-7-13 12:07

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:donjuan    来源:博客园

分享:
  在前面一文使用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_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;
  }

上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。

21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号