不知道有多少人会写js的unit test, 不过最近的项目对unittest要求比较严格.所以也养成了写unittest的习惯. 在搞js-shell时, 对写的代码没有做test,总是不放心. 找了一些资料看到JQuery的unit test方法不错. 学习了一下也在js-shell加入了Uinttest库. 总算把前面的代码都加上了UT才放心.
js-shell中的unittest的使用方法和概念都和junit差不多, 主要有TestRunner, TestResult, TestSuite, TestCaseContext几个对象. 下面是一个例子:
1. 先实现一个test runner, 创建run_unit_test.js 这是必须的步骤,不过内容很简单:
var ut = __import__('UnitTest', null, {}) //导入Unitest 库
ut.run_suite('.') //运行Testsuite, 将运行当前目录下的所有以'test_'开头的js文件作为unittest运行.
2. 实现一个unitest, 创建test_hellotest.js
test("assertEqual", function() { //一个unittest, 名称为"assertEqual", function为 test的内容.
assertEqual(10+ 20, 30) //一个比较断言,,
});
test("assertEqual fail no message", function() { //一个失败的 unitest 例子.
assertEqual(10+ 20, 31)
});
test("assertEqual fail with message", function() {
assertEqual(10+ 20, 31, "10+20 != 31")
});
test("assertRaises expect raised", function() { //测试一个将要抛出异常的方法.
assertRaises('Stop', function(m){throw m}, 'Stop')
});
test("assertRaises mismatch raised", function() {
assertRaises('Stop', function(m){throw m}, 'Start')
});
test({ //实现一个Unittest对象, 包含setUp, tearDown.
setUp: function(){ //一个setUp, 在每个test方法调用之前调用.
this.msg = "enter setUp";
info('enter setUp'); //在Test中输出消息.
},
test_basic_requirements: function(){ //一个方法内的test, 方法名必须以"test_"开头
info('running test_basic_requirements');
assertEqual(this.msg, "enter setUp")
this.msg = 'test_basic_requirements'
},
test_setUp_called_for_every_functional: function(){
info('test_setUp_called_for_every_functional');
assertEqual(this.msg, "enter setUp")
this.msg = 'test_setUp_called_for_every_functional'
},
tearDown: function(){ //在每个test调用用调用.
this.msg = "enter tearDown";
info('enter tearDown');
},
});
3. 运行unit test
C:\utest>js run_unit_test.js
[INFO]:Start test file:C:\utest\test_hellotest.js
assertEqual...PASS in 0ms
assertEqual fail no message...FAIL in 16ms
expected: 31, actual: 30
assertEqual fail with message...FAIL in 0ms
10+20 != 31
assertRaises expect raised...PASS in 0ms
assertRaises mismatch raised...FAIL in 0ms
expected exception:Stop, catched:Start
[INFO]test_setUp_called_for_every_functional:enter setUp
[INFO]test_setUp_called_for_every_functional:test_setUp_called_for_every_functio
nal
[INFO]test_setUp_called_for_every_functional:enter tearDown
test_setUp_called_for_every_functional...PASS in 31ms
[INFO]test_basic_requirements:enter setUp
[INFO]test_basic_requirements:running test_basic_requirements
[INFO]test_basic_requirements:enter tearDown
test_basic_requirements...PASS in 16ms
-----------------------------------------------------------
Ran 7 tests in 63ms
FAILED! Pass:4, Failed:3
这是一个简单的Unit, test. 测试的输出和UnitTest中的断言方法都可以自定义. 下次将介绍如何定义assert方法.
分享到:
相关推荐
Jsmaine for unit test JavaScript, 用来做JavaScript单元测试的利器,详细情况请关注博客:http://blog.csdn.net/ghostbear
JavaScript Unit Testing 190 pages Publisher: Packt Publishing; 1st New edition edition (January 14, 2013) ...•Customize JavaScript unit testing frameworks to include rich test expressions.
This book is about programming JavaScript for the real world, using the techniques and workflow suggested by Test-Driven Development. It is about gaining confidence in your code through test coverage,...
springboot-reactjs-fullstack ...使用Srping Boot和React js(React Hooks,Functional Programming,Spring Boot RESTful API,WEBPACK,BABEL JPA UNIT TEST和JEST TEST RUNNER)创建一个全栈应用程序
Cover the most popular JavaScript Unit Testing Frameworks including Jasmine, YUITest, QUnit, and JsTestDriver. Automate and integrate your JavaScript Unit Testing for ease and efficiency.
我试图使业力测试运行并遇到一些问题: 我必须在myServiceMock模块的syncCall方法中添加一个return语句。 我对指令的测试根本没有运行:我总是收到错误消息“意外的请求:GET ../templates/pablo-my-directive.html...
Javascript Unit Testing
Javascript单元测试框架Demo这个Demo主要是用来演示js的单元测试框架及一些配套工具的使用。Mocha/目录用来演示 Mocha, Mocha + chai, Mocha + chai + Karma 的使用Jasmine/目录用来演示 Jasmine, Jasmine + chai, ...
$inject ( "http://localhost/unit-test-injector/js/testiife.js" , function ( ) { QUnit . test ( "variable 'name' inside loaded IIFE should be " , function ( assert ) { assert . ok ( name === "IIFE!...
#rn-unit-test Jest+Enzyme+storybook 配置 Jest配置 1. 安装 在利用react-native init AwesomeProject创建项目的时候就会默认安装了,这里就不多介绍了。 在你使用 或 创建你的 React 或 React Native 项目时,Jest...
deferred-unit-test:示例:在生命周期方法中使用延迟对象测试异步操作
angularjs单元测试 示例angularjs项目尝试单元测试 安装 使用纱线 yarn install yarn run karma start 使用NPM npm install npm install -g karma karma start
Strapi应用 用Strapi jest集成/功能测试示例。 有关详细信息,请参见主。 理论 相关文章 -Strapi官方文档 由Mateusz Wojczal 由Mateusz Wojczal 测试状态CI / CD: (单击徽章以查看结果...npm run test或yarn test
Javascript单元测试演示 开始: 首先,请确保您已安装。 安装依赖项: $ npm install
Test-Driven Development with Python: Obey the Testing Goat: Using Django, Selenium, and JavaScript by Harry J. W. Percival English | 2 Aug. 2017 | ISBN: 1491958707 | ASIN: B074HXXXLS | 604 Pages | AZW...
kuromoji.js 日本形态分析器的 JavaScript 实现。 这是的纯 JavaScript 移植。...test/ -- Unit test 用法 您只需 5 行代码即可标记句子。 如果您需要工作示例,您可以查看 demo 或 example 目录下
Starting with reviewing the test-driven development (TDD) life cycle, you will learn how Karma and Protractor make your life easier while running JavaScript unit tests. You will learn how Protractor ...
Each unit contains an annotated tutorial and a platform where you can test your understanding of the topic. Topics Arrays Bad Practices and Solutions Booleans Built-in Functions Closure ...
npm install react-test-unit --save-devReact Test Unit提供了renderComponent函数,该函数接受React组件并返回浅表呈现的输出树。 在 ,它使用并递归渲染视图树。 import { renderComponent } from 'react-test-...
YUI Test is a complete testing framework for JavaScript and Web applications. You can use the simple JavaScript syntax to write unit tests that can be run in web browsers or on the command line, as ...