Home > Web Front-end > JS Tutorial > Detailed explanation of unit and E2E testing steps with Angular CLI

Detailed explanation of unit and E2E testing steps with Angular CLI

php中世界最好的语言
Release: 2018-05-08 16:17:05
Original
2411 people have browsed it

This time I will bring you a detailed explanation of the steps for unit and E2E testing with Angular CLI. What are the precautions for unit and E2E testing with Angular CLI? The following is a practical case, let's take a look.

Unit testing.

angular cli uses karma for unit testing.

First execute ng test --help or ng test -h to view the help.

To execute the test, just execute ng test. It will execute all the .spec.ts files in the project.

And it also It will detect file changes. If the file changes, then it will re-execute the test.

It should be executed in a separate terminal process.

First create an angular project with routing:

ng new sales --routing
Copy after login
After creating the project, directly execute the command test:

ng test
Copy after login

Then a page will pop up, which is the test result data.

Now I will add a few more components and an admin module:

ng g c person
ng g c order
ng g m admin --routing
ng g c admin/user
ng g c admin/email
Copy after login
Then configure the routing, and the most important thing is to get this effect:

At this time I will re- Execute ng test:

#Although the program runs without problems, there is still a problem in the test: router-outlet is not an angular component.

You can see Check the spec list:

At this time, because the admin module runs independently when running the test, the module does not reference the Router. module, so router-outlet cannot be recognized.

So how to solve this problem?

Open admin.component.spec.ts:

Fill in this sentence, and then there will be no errors:

##NO_ERRORS_SCHEMA tells angular to ignore those unrecognized elements or element attributes.

    --code-coverage -cc code coverage report, this is not enabled by default because the speed of generating reports is still relatively slow.
  1. --colors output results Using various colors is enabled by default
  2. --single-run -sr to execute the test, but does not detect file changes and is not enabled by default
  3. -- progress outputs the test process to the console and is enabled by default
  4. --sourcemaps -sm Generates sourcemaps is enabled by default
  5. --watch -w Run Test once, and change detection is enabled by default
  6. ng test is to run the test, and if the file changes, the test will be re-run.

Use ng test -sr or ng test -w false Execute a single test

Test code coverage:

ng test --cc's report is generated in the /coverage folder by default, but it can be modified by modifying .angular- Modify the properties in cli.json.

The code coverage report is generated below:

ng test -sr -cc
Copy after login

Usually used with the -sr parameter (run a test).

Then Some files will be generated in the coverage folder of the project:

Open index.html directly:

You can see it Everything is 100%, this is because I didn’t write any code.

Then I added some code in the user component:

再运行一次 ng test --sr -cc:

可以看到这部分代码并没有覆盖到.

如果我把代码里到 canGetUsers改为true:

再次执行ng test --sr -cc

可以看到这次代码覆盖率变化了:

只有catch部分没有覆盖到.

我认为代码覆盖率这个内置功能是非常好的.

Debug单元测试.

首先执行ng test:

然后点击debug, 并打开开发者工具:

然后按cmd+p:

找到需要调试的文件:

设置断点:

然后在spec里面也设置一个断点:

最后点击浏览器的刷新按钮即可:

E2E测试的参数.

实际上angular cli是配合着protractor来进行这个测试的.

它的命令是 ng e2e.

常用的参数有:

  1. --config -c 指定配置文件 默认是 protractor.conf.js

  2. --element-explorer -ee 打开protractor的元素浏览器

  3. --serve -s 在随机的端口编译和serve 默认true

  4. --specs -sp 默认是执行所有的spec文件, 如果想执行某个spec就使用这个参数, 默认是all

  5. --webdriver-update -wu 尝试更新webdriver 默认true

通常执行下面机组命令参数组合即可:

ng e2e
ng e2e -ee
Copy after login

Debug E2E测试.

看一下项目:

配置文件protractor.conf.js已经配置好.

而测试文件是在e2e目录下.

看一下spec和po文件:

再看一下app.component.html里面的值:

应该是没问题的.

所以执行ng e2e:

测试通过, 但是浏览器闪了一下就关闭了.

如果我想debug e2e, 那么执行这个命令:

ng e2e -ee
Copy after login

由于我使用的是mac, 当前这个命令在mac上貌似确实有一个bug:

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

Vue中v-show判断表达式如何实现

vuejs中v-if和v-show使用详解

The above is the detailed content of Detailed explanation of unit and E2E testing steps with Angular CLI. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template