当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > 详解Angular Karma测试的持续集成实践

详解Angular Karma测试的持续集成实践

2020年03月09日  | 移动技术网IT编程  | 我要评论

使用angular + karma + jasmine可以进行前端的单体测试,从前面的文章中我们了解到了karma的工作原理,它会启动一个指定种类的浏览器,然后在此浏览器中运行测试用例。如果需要进行持续集成,比如结合jenkins或者其他方式进行自动化的测试,如果需要手动关闭浏览器的操作,或者无法提供图形化的界面的情况保证测试的执行这些都会成为持续集成的障碍,这篇文章整理一下解决的常见方法。

可使用浏览器的测试环境

当测试环境可以使用浏览器,在这台机器上使用ng test则能进行测试,karma会启动chrome浏览器,然后执行测试用例,持续集成的时候,jenkins通过远程命令执行的方式到可使用浏览器的测试环境中执行ng test完成测试。

这种方式非常简单,需要解决的只有一个问题,angular 的demo应用执行ng test时,执行完毕之后,chrome浏览器也不会退出,这样jenkins的调用部分也不会返回,只需要保证其执行结束后立即关闭浏览器,这种方式就没有问题了。而实际上karma的设定文件中,singlerun正是这个选项,缺省被设定为false,这就是其不退出的原因。只需要将此选项设定为true即可。demo示例的karma设定文件改成如下即可:

liumiaocn:demo liumiao$ cp karma.conf.js karma.conf.js.origin
liumiaocn:demo liumiao$ vi karma.conf.js
liumiaocn:demo liumiao$ diff karma.conf.js karma.conf.js.origin 
29c29
<   singlerun: true,
---
>   singlerun: false,
liumiaocn:demo liumiao$

执行日志如下所示:

liumiaocn:demo liumiao$ rm -rf coverage/
liumiaocn:demo liumiao$ ls
readme.md      e2e         node_modules     src         tsconfig.spec.json
angular.json     karma.conf.js    package-lock.json  tsconfig.app.json  tslint.json
browserslist     karma.conf.js.origin package.json     tsconfig.json
liumiaocn:demo liumiao$ 
liumiaocn:demo liumiao$ ng test --code-coverage
30% building 12/12 modules 0 active31 10 2019 20:19:39.308:info [karma-server]: karma v4.1.0 server started at http://0.0.0.0:9876/
31 10 2019 20:19:39.314:info [launcher]: launching browsers chrome with concurrency unlimited
30% building 13/13 modules 0 active31 10 2019 20:19:39.324:info [launcher]: starting browser chrome
31 10 2019 20:19:43.028:info [chrome 78.0.3904 (mac os x 10.14.0)]: connected on socket pwhf3r-knkzvdi1aaaaa with id 46366297
chrome 78.0.3904 (mac os x 10.14.0): executed 3 of 3 success (0.373 secs / 0.321 secs)
total: 3 success
total: 3 success
total: 3 success

=============================== coverage summary ===============================
statements  : 100% ( 6/6 )
branches   : 100% ( 0/0 )
functions  : 100% ( 1/1 )
lines    : 100% ( 5/5 )
================================================================================
liumiaocn:demo liumiao$ ls
readme.md      coverage       karma.conf.js.origin package.json     tsconfig.json
angular.json     e2e         node_modules     src         tsconfig.spec.json
browserslist     karma.conf.js    package-lock.json  tsconfig.app.json  tslint.json
liumiaocn:demo liumiao$ 

在这个过程中可以看到chrome被自动打开、执行测试用例并显示结果然后自动退出了。通过远程方式调用应该也没有问题。不评价解决方法的好坏与局限性,这也是一种实现的方式。

无需打开浏览器的测试环境: phantomjs

比如测试环境在一个基于alpine版linux的容器之中的情况下,无法使用或者不希望使用图形化的浏览器的情况下,可以使用浏览器的headless模式或者无界面方式的浏览器。phantomjs就是这样的一种解决方法,phantom是一个隐形的浏览器,就像它的名字那样,像一个“鬼魂/幻影/幽灵”,而事实上并没有那么高深。phantomjs是基于webkit内核的headless模式,所以webkit浏览器能做的事情,基本上它都能做,在之前爬虫的一些使用场景中有需要爱好者的追随。目前稳定版本为2.1,短时间内将为稳定在这一版本,因为目前其已经暂停更新了,更新时间据说会另行通知,但是迟迟未到。使用这个暂时停更的phantomjs也是一种解决方法,具体步骤如下。

步骤1: 安装phantomjs

安装非常简单,phantomjs提供windows/linux/macos的二进制文件,只需要将相应的bin目录加入到path搜素路径中即完成了安装,此处以macos上的安装为例。

下载地址:

解压:解压至/usr/local/phantomjs下

版本确认:phantomjs --version

liumiaocn:demo liumiao$ export path=$path:/usr/local/phantomjs/phantomjs-2.1.1-macosx/bin
liumiaocn:demo liumiao$ which phantomjs
/usr/local/phantomjs/phantomjs-2.1.1-macosx/bin/phantomjs
liumiaocn:demo liumiao$ phantomjs --version
2.1.1
liumiaocn:demo liumiao$ 

步骤2: 安装karma-phantomjs-launcher

执行日志入如下所示:

liumiaocn:demo liumiao$ npm install --save-dev karma-phantomjs-launcher

> phantomjs-prebuilt@2.1.16 install /users/liumiao/desktop/demo/node_modules/phantomjs-prebuilt
> node install.js

considering phantomjs found at /usr/local/phantomjs/phantomjs-2.1.1-macosx/bin/phantomjs
found phantomjs at /usr/local/phantomjs/phantomjs-2.1.1-macosx/bin/phantomjs ...verifying
writing location.js file
phantomjs is already installed on path at /usr/local/phantomjs/phantomjs-2.1.1-macosx/bin/phantomjs
npm warn eslint-plugin-compat@3.3.0 requires a peer of eslint@^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 but none is installed. you must install peer dependencies yourself.

+ karma-phantomjs-launcher@1.0.4
added 16 packages from 36 contributors in 9.804s
liumiaocn:demo liumiao$ 

步骤3: 修改karma的配置文件

将缺省的karma配置文件做如下修改即可

liumiaocn:demo liumiao$ diff karma.conf.js karma.conf.js.origin 
10c10
<    require('karma-phantomjs-launcher'),
---
>    require('karma-chrome-launcher'),
28,29c28,29
<   browsers: ['phantomjs'],
<   singlerun: true,
---
>   browsers: ['chrome'],
>   singlerun: false,
liumiaocn:demo liumiao$ 

修改说明:

  • 仍然需要将singlerun设定为true
  • 将chrome的launcher换成phantomjs
  • 将浏览器也从chrome换成phantomjs

如果使用的是angular 8或者是es6的target设定需要将target做如下修改

liumiaocn:demo liumiao$ diff tsconfig.json tsconfig.json.org 
13c13
<   "target": "es5",
---
>   "target": "es2015",
liumiaocn:demo liumiao$ 

步骤3: 执行测试

执行ng test可以看到使用的是phantomjs进行的测试,而且在执行过程中并没有浏览器被打开和执行。

liumiaocn:demo liumiao$ ng test --code-coverage
25% building 94/94 modules 0 active01 11 2019 06:11:48.490:info [karma-server]: karma v4.1.0 server started at http://0.0.0.0:9876/
01 11 2019 06:11:48.493:info [launcher]: launching browsers phantomjs with concurrency unlimited
25% building 96/96 modules 0 active01 11 2019 06:11:48.573:info [launcher]: starting browser phantomjs
01 11 2019 06:11:52.035:info [phantomjs 2.1.1 (mac os x 0.0.0)]: connected on socket ptr3e6etdhow0jceaaaa with id 8370488
phantomjs 2.1.1 (mac os x 0.0.0): executed 3 of 3 success (0.512 secs / 0.769 secs)
total: 3 success
total: 3 success
total: 3 success

=============================== coverage summary ===============================
statements  : 100% ( 7/7 )
branches   : 100% ( 0/0 )
functions  : 100% ( 2/2 )
lines    : 100% ( 6/6 )
================================================================================
liumiaocn:demo liumiao$ 

此中方式由于phantomjs已经暂停更新,碰到问题时可能会较为尴尬,比如目前就出现在angular 8升级是phantomjs无法正常动作的网上发帖求助,实际上修改为es5能解决大部分问题,所以选择时需要慎重考虑。

无需打开浏览器的测试环境: chrome的无头模式

phantomjs使用webkit内核,无需打开浏览器来完成测试,而实际上除去ie的很多浏览器都提供这种所谓的无头(headless)模式,chrome也可以直接提供,在chrome 59开始提供了headless mode(无头模式)。在angular 8中使用chrome的无头模式进行测试非常简单,只需要修改缺省的浏览器从chrome到chromeheadless即可。

步骤1: 修改karma的配置文件

将缺省的karma配置文件做如下修改即可

liumiaocn:demo liumiao$ diff karma.conf.js karma.conf.js.origin 
28,29c28,29
<   browsers: ['chromeheadless'],
<   singlerun: true,
---
>   browsers: ['chrome'],
>   singlerun: false,
liumiaocn:demo liumiao$ 

修改说明:

  1. 仍然需要将singlerun设定为true
  2. 将浏览器从chrome改成chromeheadless

步骤2: 执行测试

执行ng test可以看到使用的是chromeheadless进行的测试,而且在执行过程中并没有浏览器被打开和执行。

liumiaocn:demo liumiao$ ng test --code-coverage
25% building 15/15 modules 0 active01 11 2019 06:37:05.037:info [karma-server]: karma v4.1.0 server started at http://0.0.0.0:9876/
01 11 2019 06:37:05.040:info [launcher]: launching browsers chromeheadless with concurrency unlimited
25% building 93/93 modules 0 active01 11 2019 06:37:05.154:info [launcher]: starting browser chromeheadless
01 11 2019 06:37:08.720:info [headlesschrome 78.0.3904 (mac os x 10.14.0)]: connected on socket br7fkzwejygns2wgaaaa with id 35869507
headlesschrome 78.0.3904 (mac os x 10.14.0): executed 3 of 3 success (0.374 secs / 0.319 secs)
total: 3 success
total: 3 success
total: 3 success

=============================== coverage summary ===============================
statements  : 100% ( 7/7 )
branches   : 100% ( 0/0 )
functions  : 100% ( 2/2 )
lines    : 100% ( 6/6 )
================================================================================
liumiaocn:demo liumiao$ 

总结

这篇文章介绍了三种常见的karma的集成方式,由于phantomjs暂停更新,并且其内核只是webkit,所以大多数情况直接使用浏览器的headless mode可能是个更好的主意。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网