自动化测试UI Test, Performance Test, Load Test 总结整理

MSDN: 测试应用程序Test apps early and often ,Improve Code Quality

推荐书: 《Visual Studio 2015高级编程》 《Visual Studio 2012应用生命周期管理高级教程》

推荐资料: DevOpsHub 文档中心

 

一、UI Test

  这里所讲的UI Test主要是基于MS的Code UI Test和Selenium。

 1.Code UI Test不仅可以给予web browser,还可以基于普通的客户端exe的程序,十分强大,不过它只适用于.net。如果用它来测Web项目,它默认只是基于IE的,其他的浏览器它会当成普通的客户端程序处理。如果需要测其他浏览器需要安装Selenium components for Coded UI Cross Browser Testing,详情可以参考Using Different Web Browsers with Coded UI Tests

参考文献:

  MSDN: Use UI Automation To Test Your Code

  如何使用Coded UI Test对Webpage进行自动化测试

 

 2.Selenium只适用于web browser,需要各个浏览器的驱动,简单说就是安装个简版的浏览器。它不仅可以用.net,还可以有java,python等等版本。它也可以录屏设置,Selenium1 和Selenium2的区别很大,Api基本不能通用。注意区分。

参考文献:

  MSDN: Get started with Selenium testing in a continuous integration pipeline

  推荐书: 《Selenium自动化测试指南》

  总结:虽然两者都可以通过录屏来编辑,不过推荐还是手动去一个控件一个控件的写,因为录屏的话会多出好多无用的操作步骤,影响我们的测试。UI test的好处可以给到前端开发框架反馈,形成公司通用的前端组件库,有利于前端规范组件化开发,和制定对应的ui test 组件模板。

 

二、Performance Test

  性能测试和压力测试是不分家,压力测试是基于一组性能测试形成的。与MS的Performance Test对应有Jmeter等,同样,Jmeter也可以做性能测试,不过它的初衷是基于应用的,这点和MS的performance Test很像。

  Performance Test是基于Http协议的,这点也是与UI Test最大的区别,所以它不需要依赖于浏览器,换句话说,就是你前端写的所有JS方法都无关,它只记录Http的请求。除此之外,倒是和UI Test挺相像的,都是基于请求响应,从返回结果中提取验证规则,判断是否成功。这里推荐使用录制生成的图形配置界面去管理,因为它的参数化,数据源等都很到位,加上自定义的validation rule完全可以应付大多数的情况,它还能生存手动的测试代码,最是方便。

  这里需要提一下,Performance的组合测试,试想一下,如果我们有一些通用的步骤,会在多个性能测试中出现,比如说登录,这就很有用了。简单的说就是把一个大的performance拆分成多个小的测试(通过Extract Web Test),来组合成一个大的测试。详情可以参考:演练:创建一个需要登录和注销的 Web 性能测试。大家可能会关系他们之间传参的问题,事实上,他们是通过webTest.Context的上下文对象传递的,就是说我之前在WebTest.Context["state"]赋了值,然后我整个performance都能拿得到这个参数。可以参考:Is it possible to pass parameters between web performance tests in visual studio 2013?

  关于登录功能,比如我需要记住token,然后在每次请求头中都带有这个token值,当然可以用上面的方法,不过还有一个更方便的办法,就是使用自定义插件。因为即使是上面的抽取公共步骤,我们还是会在接下来的每一步都要去设置这个请求头参数,比较麻烦,这时,用上Web性能测试插件就比较完美了。因为它不仅可以通过WebTest.Context获取token值,还可以重写PreRequest、PostRequest、PrePage、PostPage、PreTransaction、PreWebTest等方法。详情可以参考:如何:创建 Web 性能测试插件如何:创建请求级插件.

参考文献:

  MSDN: Performance testing

      使用 Visual Studio Web 性能和负载测试进行性能和压力测试      

      Coding a custom validation rule for a web performance test

 

三、Load Test

  压力测试主要是通过模拟请求来测试服务器的相应情况,还能监控服务器的各项性能指标。不过对于大量用户而言,单机不足以模拟大量用户,visual studio 2012还有一个限制,开发环境最多模拟250个用户。需要大量用户的话,Visual Studio支持分布式环境下的负载测试,需要安装控制器和代理服务。(P478)(使用测试控制器和测试代理在多台测试计算机之间分发负载测试运行)

  当然还有一个重点要提一下,通常在开发环境下,你可以在即装sql server,也部署服务站点,通过本机的visual studio来模拟测试。但为了更好的模仿真实环境,比较通常服务器都是分开部署的(数据库和应用站点,当然还有可能有缓存服务器,负载均衡和集群等),所以我们要有对远程服务器监控的能力。

  Visual Studio的Load Test当然只能针对于windows的服务器,关于Load Test的配置比较简单,只需在counter中添加计算机并设置监控参数(在这里提一下,默认的“ASP.NET”、“Application”、“.NET Application”、“IIS”、“SQL”等都是vs默认指派好的监控项,里面复选可能会有一些监控指标重复,当然也可以自定义),计算机可以是局域网内的计算机名,也可以是IP地址,详情可以参照如何:使用负载测试编辑器管理计数器集

  远程服务器也需要配,笔者也是试了很多次,主要是关闭防火墙(或者开发端口LoadTestCounterCategoryNotFoundException),添加测试用户账号并给予监控或管理员权限,开启监控服务器的监控进程等,详情可以参考Reading Performance Counters on Vista and Server 2008 Machines(第四步看不懂可以不用)。

原文摘录:

In order to allow remote performance counter collection for a machine, you need to follow these steps on the machine you are collecting from. Note if you are using a test controller, the user account is the account the controller service is running as. If you are running load tests locally, the counters are collected using your user credentials.

    1. Run WF.msc 
      1. Enable the Performance Logs & Alerts firewall exception
      2. On Vista, enable the File and Printer Sharing firewall exception
    2. Run lusrmgr.msc
      1. Add the user to the Performance Log Users, Performance Monitor Users, or Administrators group.
      2. Add the user to the Event Log Readers group
    3. Run services.msc 
      1. Set the Performance Logs & Alerts service to autorun
      2. Set the Remote Registry service to autorun
    4. Run secpol.msc to assign the Log on as a Batch User privilege to Performance Log Users and Performance Monitor Users group:
      1. In Local Security Policy navigate to Local Policies -> User Right Assignment
      2. Double-click on “Log on as batch job”
      3. Add Performance Log Users group
      4. Add Performance Monitor Users group

Open a command prompt and rebuild all counters on the machine by running

lodctr /r

参考文献:

  MSDN:使用 Visual Studio Web 性能和负载测试进行性能和压力测试      

      在负载测试分析器的表视图中分析负载测试结果和错误      

      如何:使用负载测试编辑器管理计数器集

      最省錢及省時的負載壓力測試怎麼做? Visual Studio Online 雲端壓力測試 (Cloud Load Test) 簡介

  Performance Monitor

  磁盘性能计数器的使用方法

 

大家如果想获得更多的控制能力,可以参考为负载测试创建自定义代码和插件,或者直接调用Visual Studio ALM 测试工具的 API

源码:http://pan.baidu.com/s/1mh7v3ks(涉及公司项目,就不公开了)

posted @ 2016-12-29 14:33  海~~D  阅读(5280)  评论(0编辑  收藏  举报