Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何通过NodeJS和Mocha执行不同结构的不同测试用例_Javascript_Node.js_Selenium_Selenium Webdriver_Mocha.js - Fatal编程技术网

Javascript 如何通过NodeJS和Mocha执行不同结构的不同测试用例

Javascript 如何通过NodeJS和Mocha执行不同结构的不同测试用例,javascript,node.js,selenium,selenium-webdriver,mocha.js,Javascript,Node.js,Selenium,Selenium Webdriver,Mocha.js,如何通过NodeJS和Mocha执行不同结构的不同测试用例 向前迈进,我打算整合Selenium+

如何通过
NodeJS
Mocha
执行不同结构的不同测试用例

向前迈进,我打算整合Selenium+
+

我刚开始用摩卡探索NodeJS,需要一些帮助

  • 已安装的
    node.js

    C:\Users\AtechM_03>node -v
    v6.11.2
    
  • 已安装的
    npm

    C:\Users\AtechM_03>npm -v
    3.10.10
    
  • 按此配置
    nodeclipse
    ,我的项目结构如下:

  • 按照在默认位置(通过命令行)安装
    Mocha

  • 接着在NodeJS中编写了一个集成Mocha的程序

  • NodeProject
    空间中创建了名为
    test
    的目录

  • 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>
    
  • 将代码添加到
    test.js
    ,如下所示:

        {
          "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"
        }
    
            // 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);
                  });
              });
    
            });
    
            {
              "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"
            }
    
        C:\Users\AtechM_03\LearnAutmation\NodeProject>npm test
    
        > temperature@1.0.0 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)
    
  • 从成功运行的项目空间执行
    npm测试

            C:\Users\AtechM_03\LearnAutmation\NodeProject>npm test
    
            > temperature@1.0.0 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)
    
  • 接着在NodeJS中编写了第二个集成Mocha的程序

  • NodeProject
    空间中创建了一个名为
    temperature
    的单独目录
  • temperature
    目录中创建了一个名为
    app.js
    的文件和一个文件夹名
    test
  • 在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)
    
  • 当前
    温度
    测试用例如下所示:

  • 试图通过
    npm test
    从项目空间执行第二个程序,但仍按如下方式执行第一个程序:

        {
          "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"
        }
    
            // 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);
                  });
              });
    
            });
    
            {
              "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"
            }
    
        C:\Users\AtechM_03\LearnAutmation\NodeProject>npm test
    
        > temperature@1.0.0 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通过(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;
            }
    

    根据我下面的内容,我预计会出现一个错误,即
    0通过(20ms)

    您有一个很长的描述,但我从中提取的东西是,您基本上有这样一个结构:

    NodeProject
    ├── temperature
    │   └── test
    └── test
    
    然后进入
    NodeProject
    并运行Mocha。默认情况下,Mocha将只在调用它的同一目录中查找
    test
    。因此它不会查找
    温度/测试
    。如果你想让摩卡在
    温度/测试中运行测试,你必须明确告诉摩卡。例如,这将起作用:

    mocha test temperature/test
    
    我将在这里讨论一个常见的误解,因为我经常看到人们犯错误:仅仅使用
    --recursive
    标志是不够的。如果您使用这个标志,那么在Mocha确定了要在其中查找测试的目录之后,它将递归地查找这些目录。然而,它并没有改变Mocha如何识别在其中查找测试的目录。具体地说,如果使用
    mocha--recursive
    ,它仍然只会在
    test
    中查找,并且会在
    test
    的子目录中查找。这不会使其在
    温度/测试中显示。如果在
    test
    temperature/test
    中确实有子目录,则可以执行以下操作:

    mocha --recursive test temperature/test
    

    你能展示你的app.js有什么内容吗?@HarshPatel根据你的评论更新了问题。你的app.js文件中没有写描述或it方法/函数。因此,它不会向控制台显示任何输出。但它肯定会执行。