All Projects → zswang → examplejs

zswang / examplejs

Licence: MIT license
A tool for converting example code into test cases

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to examplejs

Vim Js
💯The most accurate syntax highlighting plugin for JavaScript and Flow.js
Stars: ✭ 99 (+125%)
Mutual labels:  jsdoc
Type O Rama
👾 JS type systems interportability
Stars: ✭ 217 (+393.18%)
Mutual labels:  jsdoc
dumbmutate
Simple mutation-testing
Stars: ✭ 32 (-27.27%)
Mutual labels:  unittest
Vanillajs Deck
A Vanilla.js Single Page App (SPA) slide deck for a presentation about Vanilla.js written with no frameworks.
Stars: ✭ 119 (+170.45%)
Mutual labels:  jsdoc
Jsdoc Vuejs
📖 A JSDoc plugin for documenting .vue files.
Stars: ✭ 186 (+322.73%)
Mutual labels:  jsdoc
Esdoc
ESDoc - Good Documentation for JavaScript
Stars: ✭ 2,706 (+6050%)
Mutual labels:  jsdoc
Jsdoctest
Run jsdoc examples as doctests.
Stars: ✭ 80 (+81.82%)
Mutual labels:  jsdoc
HTMLTestRunner
Modern style test report based on unittest framework.
Stars: ✭ 128 (+190.91%)
Mutual labels:  unittest
Docdown
A simple JSDoc to Markdown generator.
Stars: ✭ 191 (+334.09%)
Mutual labels:  jsdoc
JMemoryBuddy
No description or website provided.
Stars: ✭ 44 (+0%)
Mutual labels:  unittest
Doxdox
📚 JSDoc to Markdown, Bootstrap, and custom Handlebars template documentation generator.
Stars: ✭ 139 (+215.91%)
Mutual labels:  jsdoc
Vscode Fundamentals
👨‍🏫 Mike's Visual Studio Code Course
Stars: ✭ 175 (+297.73%)
Mutual labels:  jsdoc
FineCodeCoverage
Visualize unit test code coverage easily for free in Visual Studio Community Edition (and other editions too)
Stars: ✭ 391 (+788.64%)
Mutual labels:  unittest
Vue Docgen Api
Toolbox to extract information from Vue component files for documentation generation purposes.
Stars: ✭ 100 (+127.27%)
Mutual labels:  jsdoc
hypatia
Convert JavaScript doctrings (in jsdoc AST format) to ijavascript Jupyter Notebooks
Stars: ✭ 12 (-72.73%)
Mutual labels:  jsdoc
Awesome Web Tutorial
「JavaScript学习资料整理」系列,构建JavaScript前端知识体系,积累JavaScript前端开发经验
Stars: ✭ 84 (+90.91%)
Mutual labels:  jsdoc
Openapi Comment Parser
⚓️ JSDoc Comments for the OpenAPI Specification
Stars: ✭ 221 (+402.27%)
Mutual labels:  jsdoc
c-project-template
A C project template with Makefile, command line options parsing, unittest using cmocka and valgrind
Stars: ✭ 97 (+120.45%)
Mutual labels:  unittest
laziest
Work in Progress: Package that trying to generate unit tests from code
Stars: ✭ 18 (-59.09%)
Mutual labels:  unittest
elm-doctest
doctest runner against Elm-lang source files
Stars: ✭ 13 (-70.45%)
Mutual labels:  unittest

如何优雅地写测试用例?

NPM version Build Status Coverage Status

背景

做好单元测试是保证代码质量的有效手段。这里介绍一种用示例代码转为测试用例的工具。 这样做可以降低写测试用例的学习和使用成本。

思路

通常在示例代码中我们会用注释给出一个输出预判。

var a = 1;
var b = 2;
console.log(a === b); // false

这样方便复制到控制台编辑运行。

如果写测试用例就会这样:

var a = 1;
var b = 2;
assert.equal(a === b, false);

可以看出来:示例代码和测试代码都有一个输出预判!

那么为何不能用示例代码写测试用例呢?也就是做一下简单的转换即可。

只要做简单的约定就能达到,分别是:

  • 示例代码块
  • 输出预判
  • 异步完成
  • 异常预判

方法

定义示例代码块

jsdoc 中用 @example 标记,例如:

/**
 * @example xxyy
 * var a = 1;
 * var b = 2;
 * console.log(a === b); // false
 */

但这种每行都有 * 的代码,复制粘贴后还需要清理一次,所以我倾向于这种:

/**
 * @example xxyy
  var a = 1;
  var b = 2;
  console.log(a === b); // false
 */

为区分运行环境,所以得指定语言,如:

/**
 * @example xxyy
  ```js
  var a = 1;
  var b = 2;
  console.log(a === b); // false
  ```
 */

约定

输出预判

image

/**
 * @example 表达式相等预判
  ```js
  var a = 1;
  var b = 2;
  console.log(a === b);
  // > false
  ```
 */

/**
 * @example 表达式结果预判
  ```js
  var a = 1;
  var b = 2;
  console.log(a + b);
  // > 3
  ```
 */


/*
 * @example 表达式类型预判
  ```js
  var a = 1;
  console.log(JSON.stringify(a + '1'));
  // > "11"
  ```
 */

批量输出预判

image

/*
 * @example 批量表达式预判
  ```js
  for (var i = 0; i < 5; i++) {
    console.log(i);
  }
  // > 0
  // > 1
  // > 2
  // > 3
  // > 4
  ```
 */

异步完成

image

/*
 * @example 异步执行预判
  ```js
  var a = 1;
  setTimeout(function () {
    console.log(a);
    // > 2
    // * done
  }, 1000);
  a++;
  ```
 */

异常预判

image

/*
 * @example 异常执行预判
  ```js
  var a = JSON.parse('#error');
  // * throw
  ```
*/

浏览器环境

/*
 * @example 浏览器环境
  ```html
  <div></div>
  ```
  ```js
  console.log(document.querySelector('div') !== null);
  // > true
  ```
 */

jQuery

image

/*
 * @example jQuery
   ```css
   .red {
     background-color: red;
   }
   ```
   ```html
   <div class="red"></div>
   <script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
   ```
   ```js
   $(function () {
     console.log($('.red').css('background-color'));
     // > red
     // * done
   })
   ```
 */

使用方法

安装

$ npm install examplejs -g

运行

$ examplejs main.js -o test/main.test.js

Gulp

var examplejs = require('gulp-examplejs');

gulp.task('example-js', function() {
  return gulp.src('src/**.js')
    .pipe(examplejs({
      head: 'head.js'
    }))
    .pipe(gulp.dest('test'));
});

License

MIT © zswang

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].