如何通过 NodeJS
和Mocha
执行具有不同结构的不同测试用例。
继续前进我打算整合 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
,我的项目结构如下:
根据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
`-- mocha@3.5.3
跟随this link在NodeJS中编写一个集成Mocha的程序。
在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
> 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)
跟随this link在NodeJS中编写第二个集成Mocha的程序。
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
测试用例如下所示:
试图从项目空间通过npm test
执行第二个程序,但它仍然按如下方式执行第一个程序:
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 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)
答案 0 :(得分:3)
你有很长的描述,但我从中提取的是你基本上有这样的结构:
NodeProject
├── temperature
│ └── test
└── test
然后你进入NodeProject
并运行摩卡。默认情况下,Mocha将仅在调用它的同一目录中查找test
。所以它不会寻找temperature/test
。如果你想让Mocha在temperature/test
中运行测试,你必须明确告诉Mocha。例如,这将起作用:
mocha test temperature/test
我会在这里解决一个常见的误解,因为我经常看到人们犯了错误:仅使用--recursive
标志是不够的。如果您使用此标志,那么在 Mocha之后已经确定了要在其中查找测试的目录,它将以递归方式查看它们。但是,它不会改变Mocha如何识别要在其中查找测试的目录。具体来说,如果您使用mocha --recursive
,它仍然只会查看test
,它会显示在test
的子目录中。这不会使它看起来temperature/test
。如果您确实在test
和temperature/test
中有子目录,则可以执行以下操作:
mocha --recursive test temperature/test