如何通过 NodeJS
和Mocha
执行具有不同结构的不同测试用例。
展望 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
nodeclipse
,我的项目结构如下: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]
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)
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
> [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
中。如果确实在test
和temperature/test
中有子目录,则可以执行以下操作:mocha --recursive test temperature/test