All Projects → tank0317 → Unit Test Demo

tank0317 / Unit Test Demo

一步一步介绍如何给项目添加单元测试

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Unit Test Demo

react-ssr-spa
Server side rendered single page app using reactjs official libraries.
Stars: ✭ 30 (-96.08%)
Mutual labels:  mocha, karma
tropic
🍍 Test Runner Library
Stars: ✭ 29 (-96.21%)
Mutual labels:  mocha, karma
react-multi-context
Manage multiple React 16 contexts with a single component.
Stars: ✭ 19 (-97.52%)
Mutual labels:  mocha, travis-ci
phpunit-travis-ci-coverage-example
phpUnit Testing on Travis-CI with Code Coverage on CodeCov
Stars: ✭ 30 (-96.08%)
Mutual labels:  travis-ci, codecov
Ios Project Template
iOS project template with fastlane lanes, Travis CI jobs and GitHub integrations of Codecov, HoundCI for SwiftLint and Danger
Stars: ✭ 358 (-53.26%)
Mutual labels:  travis-ci, codecov
alexa-skill-boilerplate
An easy to use Amazon Alexa Skill Boilerplate for fast skill creation
Stars: ✭ 54 (-92.95%)
Mutual labels:  mocha, travis-ci
react-innertext
Returns the innerText of a React JSX object.
Stars: ✭ 37 (-95.17%)
Mutual labels:  mocha, travis-ci
CPP Template
C++ project template : CMake, Test, Travis CI, Appveyor, CodeCoverage, Doxygen
Stars: ✭ 32 (-95.82%)
Mutual labels:  travis-ci, codecov
Hexagon
Hexagon is a microservices toolkit written in Kotlin. Its purpose is to ease the building of services (Web applications, APIs or queue consumers) that run inside a cloud platform.
Stars: ✭ 336 (-56.14%)
Mutual labels:  travis-ci, codecov
Cpp Project
Boiler plate template for C++ projects, with CMake, Doctest, Travis CI, Appveyor, Github Actions and coverage reports.
Stars: ✭ 328 (-57.18%)
Mutual labels:  travis-ci, codecov
SpringBootRestAPI
A ready-to-use Template for Rest API using spring-boot-microservices, MongoDB as Database, Integrated with codecov and sonarqube, deployable to cloud.
Stars: ✭ 24 (-96.87%)
Mutual labels:  travis-ci, codecov
Typescript Webpack Starter
⚡ create-ts-lib: A Starter Kit and a CLI to create your TypeScript / ES6 module bundled by Webpack without thinking about build or unit tests configurations. 🏠
Stars: ✭ 358 (-53.26%)
Mutual labels:  karma, unit-test
generator-speedseed
Oriented to components, allow create/choice template, multiple configuration with easy maintenance
Stars: ✭ 13 (-98.3%)
Mutual labels:  mocha, karma
cpp14-project-template
A simple, cross-platform, and continuously integrated C++14 project template
Stars: ✭ 64 (-91.64%)
Mutual labels:  travis-ci, codecov
torchjs
Test framework to light up the world.
Stars: ✭ 40 (-94.78%)
Mutual labels:  mocha, unit-test
Karma-mocha-example
Karma to drive mocha
Stars: ✭ 19 (-97.52%)
Mutual labels:  mocha, karma
aria-vue
Testing tools for Vue components
Stars: ✭ 21 (-97.26%)
Mutual labels:  mocha, unit-test
javascript-unit-testing-with-mocha
Unit test using Mocha and Chai
Stars: ✭ 104 (-86.42%)
Mutual labels:  mocha, unit-test
Covr
Test coverage reports for R
Stars: ✭ 285 (-62.79%)
Mutual labels:  travis-ci, codecov
Ci helloworld
A simple example of how to setup a complete CI environment for C and C++
Stars: ✭ 357 (-53.39%)
Mutual labels:  travis-ci, codecov

unit-test-demo

travis-ci codecov

一步一步介绍如何给项目添加单元测试。


关于单元测试

为什么做单元测试?测试可以验证代码的正确性,当然手工也可以测试,但是这是一次性的事情,下次测试还需要从头来过,效率不能得到保证。通过编写测试用例,可以做到一次编写,多次运行。

单元测试是什么?意义?不是我们今天想讨论的重点,今天只是聊一聊怎么做单元测试。如果你最近也对单元测试是怎么做的感兴趣,这篇文章可能是你需要的。

这篇文章在讲什么?

该文章通过五个环节一步一步介绍了如何给项目添加单元测试。文章中会涉及到 Mocha, Chai, Karma, Travis-CI, Istanbul, Codecov 等,如果你还不知道这些名词是什么,或者还不知道他们是如何配合起来工作的,你可以通过以下几个环节逐步了解。

  1. 第一个环节:使用 Mocha Chai 编写测试用例
  2. 第二个环节:使用 Karma 配置测试环境。
  3. 第三个环节: 配置 Travis-CI,每次 push 自动执行测试代码。
  4. 第四个环节:添加 Codecov,自动上传测试覆盖率
  5. 第五个环节:添加徽章,如何调试测试代码,Github Status

以上的每一个环节与项目中的 Commit 一一对应

unit-test-demo-commit

同时,该项目是渐进增强的,每个环节会给项目添加新的能力,又都是独立可运行的完整示例。 每个环节都有对应的 README.md 介绍该环节执行的结果是什么以及是如何做到的。你也可以借助 Git 查看每个环节具体修改了哪些地方。

readme-example

如何快速查看每个环节的内容?

如果在 Github 上你可以直接通过 Tags 进入对应的环节,如下图所示。

unit-test-tags

如果已经将项目克隆到本地,你可以通过 git checkout Chapter* 检出你想要查看的环节。

推荐的学习方式

如果你跟我一样一开始并不了解单元测试,推荐将项目克隆到本地,检出每个环节对应的 Commit ,直观查看执行的结果。只有程序都跑通了,我们再谈是怎么做到的。

对于前两个环节你可以直接在本地运行命令查看结果,但是对于第三和第四个环节,需要 Push 代码到 Github 才能看到效果。所以你可以fork 项目到自己的 Github,基于第二个环节对应的 Commit 新建一个分支,在新的分支上进行操作,然后 Push 代码到自己的仓库,查看是否有预期效果。

通过这篇文章你能够学到什么?

通过该文章你不仅仅能够学会如何给项目添加单元测试,同时能够了解到:

  • Mocha 和 Chai 的基本使用方式;
  • Karma 是什么,如何配置 Karma;
  • Travis-CI 是什么,怎么工作的,如何配置 Travis-CI;
  • 如何获得测试覆盖率,Codecov 是什么,大致的工作原理;
  • 大概了解 Travis-CI 和 Codecov 是怎么和 Github 配合工作的。

最后

一周之前我也不清楚单元测试是什么,怎么做的。查看了很多文档后感觉自己大致了解了这一套流程是怎么工作的,所以记录下来供大家参考。

如果文章里有哪些理解不对,欢迎指正。一起努力,共同进步~

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].