如何通过 NodeJSMocha 执行具有不同结构的不同测试用例。

展望 future ,我打算集成 Selenium + NodeJS + Mocha

我刚刚开始使用Mocha探索NodeJS,需要一些帮助。

  • 已安装node.js:
    C:\Users\AtechM_03>node -v
    v6.11.2
    
  • 已安装npm:
    C:\Users\AtechM_03>npm -v
    3.10.10
    
  • 按照此link配置的nodeclipse,我的项目结构如下:

  • javascript - 如何通过Node JS和Mocha执行具有不同结构的不同测试用例-LMLPHP
  • 根据this link在默认位置(通过命令行)安装了Mocha
    C:\Users\AtechM_03>npm install -g mocha
    C:\Users\AtechM_03\AppData\Roaming\npm\mocha -> C:\Users\AtechM_03\AppData\Roaming\npnode_modules\mocha\bin\mocha
    C:\Users\AtechM_03\AppData\Roaming\npm\_mocha -> C:\Users\AtechM_03\AppData\Roaming\n\node_modules\mocha\bin\_mocha
    C:\Users\AtechM_03\AppData\Roaming\npm
    `-- [email protected]
    
  • 跟随this link在集成Mocha的NodeJS中编写程序。
  • test空间中创建了一个名为NodeProject的目录。
  • test文件夹内创建了一个名为test.js的文件
  • 执行npm init以交互方式创建package.json文件。
        C:\Users\AtechM_03>cd C:\Users\AtechM_03\LearnAutmation\NodeProject
    
        C:\Users\AtechM_03\LearnAutmation\NodeProject>npm init
        This utility will walk you through creating a package.json file.
        It only covers the most common items, and tries to guess sensible defaults.
    
        See `npm help json` for definitive documentation on these fields
        and exactly what they do.
    
        Use `npm install <pkg> --save` afterwards to install a package and
        save it as a dependency in the package.json file.
    
        Press ^C at any time to quit.
        name: (NodeProject) test
        version: (1.0.0) 1.0.0
        description: test123
        entry point: (index.js) test.js
        test command: (mocha) mocha
        git repository:
        keywords:
        author: debanjan
        license: (ISC)
        About to write to C:\Users\AtechM_03\LearnAutmation\NodeProject\package.json:
    
        {
          "name": "test",
          "version": "1.0.0",
          "description": "test123",
          "main": "test.js",
          "directories": {
            "test": "test"
          },
          "dependencies": {
            "g": "^2.0.1",
            "selenium-webdriver": "^3.5.0"
          },
          "devDependencies": {
            "mocha": "^3.5.3"
          },
          "scripts": {
            "test": "mocha"
          },
          "author": "debanjan",
          "license": "ISC"
        }
    
    
        Is this ok? (yes)
    
    C:\Users\AtechM_03\LearnAutmation\NodeProject>
    
  • package.json在项目范围内生成,即在C:\Users\AtechM_03\LearnAutmation\NodeProject下生成,如下所示:
        {
          "name": "test",
          "version": "1.0.0",
          "description": "test123",
          "main": "test.js",
          "directories": {
            "test": "test"
          },
          "dependencies": {
            "g": "^2.0.1",
            "selenium-webdriver": "^3.5.0"
          },
          "devDependencies": {
            "mocha": "^3.5.3"
          },
          "scripts": {
            "test": "mocha"
          },
          "author": "debanjan",
          "license": "ISC"
        }
    
  • test.js添加了代码,如下所示:
            // Require the built in 'assertion' library
            var assert = require('assert');
            // Create a group of tests about Arrays
            describe('Array', function() {
              // Within our Array group, Create a group of tests for indexOf
              describe('#indexOf()', function() {
                // A string explanation of what we're testing
                it('should return -1 when the value is not present', function(){
                  // Our actual test: -1 should equal indexOf(...)
                  assert.equal(-1, [1,2,3].indexOf(4));
                });
              });
    
            //Create a test suite (group) called Math
              describe('Math', function() {
                  // Test One: A string explanation of what we're testing
                  it('should test if 3*3 = 9', function(){
                // Our actual test: 3*3 SHOULD EQUAL 9
                assert.equal(9, 3*3);
                  });
                  // Test Two: A string explanation of what we're testing
                  it('should test if (3-4)*8 = -8', function(){
                // Our actual test: (3-4)*8 SHOULD EQUAL -8
                assert.equal(-8, (3-4)*8);
                  });
              });
    
            });
    
  • 从项目空间执行的npm test成功运行:
            C:\Users\AtechM_03\LearnAutmation\NodeProject>npm test
    
            > [email protected] test C:\Users\AtechM_03\LearnAutmation\NodeProject
            > mocha
    
    
    
              Array
                #indexOf()
                  v should return -1 when the value is not present
                Math
                  v should test if 3*3 = 9
                  v should test if (3-4)*8 = -8
    
    
              3 passing (18ms)
    
  • 跟随this link在集成Mocha的NodeJS中编写第二个程序。
  • temperature空间中创建了一个名为NodeProject的单独目录。
  • temperature目录中创建了一个名为app.js的文件和一个名为test的文件夹
  • 在测试文件夹中,创建了一个名为test.js的文件
  • 将先前的package.json移动到一个子目录,并执行npm init再次以交互方式创建一个新的package.json文件。
            C:\Users\AtechM_03>cd C:\Users\AtechM_03\LearnAutmation\NodeProject
    
            C:\Users\AtechM_03\LearnAutmation\NodeProject>npm init
            This utility will walk you through creating a package.json file.
            It only covers the most common items, and tries to guess sensible defaults.
    
            See `npm help json` for definitive documentation on these fields
            and exactly what they do.
    
            Use `npm install <pkg> --save` afterwards to install a package and
            save it as a dependency in the package.json file.
    
            Press ^C at any time to quit.
            name: (NodeProject) temperature
            version: (1.0.0) 1.0.0
            description: temp
            entry point: (index.js) app.js
            test command: (mocha) mocha
            git repository:
            keywords:
            author: debanjanb
            license: (ISC)
            About to write to C:\Users\AtechM_03\LearnAutmation\NodeProject\package.json:
    
            {
              "name": "temperature",
              "version": "1.0.0",
              "description": "temp",
              "main": "app.js",
              "directories": {
                "test": "test"
              },
              "dependencies": {
                "g": "^2.0.1",
                "selenium-webdriver": "^3.5.0"
              },
              "devDependencies": {
                "mocha": "^3.5.3"
              },
              "scripts": {
                "test": "mocha"
              },
              "author": "debanjanb",
              "license": "ISC"
            }
    
    
        Is this ok? (yes)
    
  • 如下创建新的package.json:
            {
              "name": "temperature",
              "version": "1.0.0",
              "description": "temp",
              "main": "app.js",
              "directories": {
                "test": "test"
              },
              "dependencies": {
                "g": "^2.0.1",
                "selenium-webdriver": "^3.5.0"
              },
              "devDependencies": {
                "mocha": "^3.5.3"
              },
              "scripts": {
                "test": "mocha"
              },
              "author": "debanjanb",
              "license": "ISC"
            }
    
  • 当前的temperature测试用例如下:

  • javascript - 如何通过Node JS和Mocha执行具有不同结构的不同测试用例-LMLPHP
  • 尝试通过Project空间中的npm test执行此第二个程序,但是它仍然执行第一个程序,如下所示:
        C:\Users\AtechM_03\LearnAutmation\NodeProject>npm test
    
        > [email protected] test C:\Users\AtechM_03\LearnAutmation\NodeProject
        > mocha
    
    
    
          Array
            #indexOf()
              v should return -1 when the value is not present
            Math
              v should test if 3*3 = 9
              v should test if (3-4)*8 = -8
    
    
          3 passing (18ms)
    

  • 问题 :

    我知道我的第二个程序app.js不完整,执行它会显示错误(例如0 passing (20ms)),但是我的app.js根本没有被调用。

    有人可以指导/建议我在这里做错了什么吗?

    任何建议/指南/指针都将有所帮助。

    更新:

    截至目前,我当前的app.js代码尚不完整,其中包含以下代码:
            cToF = function(celsius) {
              if(!Number.isInteger(celsius)) return undefined;
              return celsius * 9 / 5 + 32;
            }
    
            fToC = function(fahrenheit) {
              if(!Number.isInteger(fahrenheit)) return undefined;
              return (fahrenheit - 32) * 5 / 9;
            }
    

    按照this website我正在跟踪我期望出现0 passing (20ms)错误

    最佳答案

    您已经进行了很长的描述,但是我从中提取的内容是,您基本上具有这样的结构:

    NodeProject
    ├── temperature
    │   └── test
    └── test
    

    然后进入NodeProject并运行Mocha。默认情况下,Mocha将仅在调用它的目录中查找test。因此,它不会寻找temperature/test。如果您希望Mocha以temperature/test运行测试,则必须明确告知Mocha。例如,这将起作用:
    mocha test temperature/test
    

    我将在这里解决一个常见的误解,因为我经常看到人们犯错:仅使用--recursive标志是不够的。如果使用此标志,则Mocha在确定要在其中查找测试的目录之后,它将以递归方式查找它们。但是,它不会更改Mocha识别在其中查找测试的目录的方式。具体来说,如果您使用mocha --recursive,它将仍然仅在test中查找,并且将在test的子目录中查找。这不会使它看起来在temperature/test中。如果确实在testtemperature/test中有子目录,则可以执行以下操作:
    mocha --recursive test temperature/test
    

    10-08 13:22