zoukankan      html  css  js  c++  java
  • 单元测试

    mocha提供TDD(测试驱动开发)和BDD(行为驱动开发)的风格
    TDD 关注所有功能是否被正确实现,每个功能都对应有测试用例,。偏向于说明书的风格表达方式。
    BDD 关注整体行为是否符合预期,适合自顶向下的设计方式。更接近自然语言的表达方式。
     
    npm install -g mocha
    npm install should
     
    mocha作为一个集成框架,可以使用should作为一个断言库,也可以使用nodejs自带的assert模块
    实例:
    var assert = require("assert");
    describe('Array', function(){
            describe('#indexOf()', function(){
                    it('should return -1 when the value is not present', function(){
                    assert.equal(-1, [1,2,3].indexOf(5));
                        assert.equal(-1, [1,2,3].indexOf(0));
                    })
            })});
    使用should
    require("should");
     
    var name = "zhaojian";
     
    describe("Name", function() {
        it("The name should be zhaojian", function() {
            name.should.eql("zhaojian");
        });
    });
    describe("InstanceOf", function() {
        it("Zhaojian should be an instance of Person", function() {
            zhaojian.should.be.an.instanceof(Person);
        });
     
        it("Zhaojian should be an instance of Object", function() {
            zhaojian.should.be.an.instanceof(Object);
        });
    });
    describe("Property", function() {
        it("Zhaojian should have property name", function() {
            zhaojian.should.have.property("name");
        });
    });
    对于嵌套要声明done,一个it中只能调用一次done
    fs = require('fs');
    describe('File', function(){
            describe('#readFile()', function(){
            it('should read test.ls without error', function(done){
                    fs.readFile('test.ls', function(err){
                                    if (err) throw err;
                                    done();
                            });
                    })
            })})
    Before  &  After
    describe('hooks', function() {
     
      before(function() {
        // runs before all tests in this block
      });
     
      after(function() {
        // runs after all tests in this block
      });
     
      beforeEach(function() {
        // runs before each test in this block
      });
     
      afterEach(function() {
        // runs after each test in this block
      });
     
      // test cases});
    延迟执行
    setTimeout(function() {
      // do some setup
      describe('my suite', function() {
        // ...
      });
      run();}, 5000);
     
  • 相关阅读:
    windows下Mysql免安装版,修改my_default.ini配置文件无效的解决办法
    下压桟(LIFO)
    Dijkstra的双栈算术表达式求值算法
    获取中文的完整拼音并输出
    解析一个文件夹所有文件的中文,并输出到某一文本文档中
    在含有中英文字符串的信息中,提出中文的方法
    创建计算字段
    Docker 常用命令
    mqtt常用命令及配置
    LOG4J
  • 原文地址:https://www.cnblogs.com/wanglao/p/11162681.html
Copyright © 2011-2022 走看看