• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

如何进行angular的单元测试?

angularjs 搞代码 4年前 (2021-12-31) 37次浏览 已收录 0个评论

隔离程序的每个部件,在隔离环境中运行测试。这种测试形式叫作单元测试。在单元测试中,所写的测试需要事先提供既定的输入值与相应的逻辑单元,检测输出结果,确定它是否与我们的预期结果匹配。

下面来看一下Angular单元测试。

使用karma进行单元测试:

创建一个新的项目

ng new project --routing

运行ng test –help 可以看到测试相关的执行参数

options:
  --browsers
    Override which browsers tests are run against.
  --code-coverage
    Output a code coverage report.
  --code-coverage-exclude
    Globs to exclude from code coverage.
  --configuration (-c)
    A named configuration environment, as specified in the "configurations" section of angular.json.
  --environment
    Defines the build environment.
  --eval-source-map
    Output in-file eval sourcemaps.
  --help
    Shows a help message for this command in the console.
  --karma-config
    The name of the Karma configuration file.
  --main
    The name of the main entry-point file.
  --poll
    Enable and define the file watching poll time period in milliseconds.
  --polyfills
    The name of the polyfills file.
  --preserve-symlinks
    Do not use the real path when resolving modules.
  --prod
    When true, sets the build configuration to the production environment.
    All builds make use of bundling and limited tree-shaking, A production build also 
    runs limited dead code elimination using UglifyJS.
  --progress
    Log progress to the console while building.
  --reporters
    Karma reporters to use. Directly passed to the karma runner.
  --source-map
    Output sourcemaps.
  --ts-config
    The name of the TypeScript configuration file.
  --vendor-source-map
    Resolve vendor packages sourcemaps.
  --wa<em style="color:transparent">来源[email protected]搞@^&代*@码网</em>tch
    Run build when files change.

NO_ERRORS_SCHEMA告诉angular忽略那些不识别的元素或者元素属性.

--code-coverage -cc 代码覆盖率报告, 默认这个是不开启的, 因为生成报告的速度还是比较慢的.
--colors 输出结果使用各种颜色 默认开启
--single-run -sr 执行测试, 但是不检测文件变化 默认不开启
--progress 把测试的过程输出到控制台 默认开启
--sourcemaps -sm 生成sourcemaps 默认开启
--watch -w 运行测试一次, 并且检测变化 默认开启
ng test 就是运行测试, 并且如果文件有变化, 就会重新运行测试.

使用ng test -sr或者ng test -w false 执行单次测试

下面生成代码覆盖率报告:

ng test -sr -cc

测试程序有两种主要方法:端对端测试和单元测试。

端对端测试:

如果使用自上而下的方法进行测试,那么写测试时就将程序视为一个“黑盒”。与程序交互就如真实用户一样,从“旁观者”的角度评判程序是否达标。这种自上而下的测试技巧被称为端对端测试。

在Angular中,最常用的工具叫作Protractor。Protractor能够打开浏览器与程序交互,收集测试结果,并检验测试结果与预期值是否相符。

单元测试:

第二种常用的测试方法是隔离程序的每个部件,在隔离环境中运行测试。这种测试形式叫作单元测试。

在单元测试中,所写的测试需要事先提供既定的输入值与相应的逻辑单元,检测输出结果,确定它是否与我们的预期结果匹配。

想要了解更多angular-js的相关知识,可访问 前端开发学习!!

以上就是如何进行angular的单元测试?的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:如何进行angular的单元测试?

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址