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
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)
NodeProject
空间中创建了一个名为temperature
的单独目录李>
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)
温度测试用例如下所示:
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方法/函数。因此,它不会向控制台显示任何输出。但它肯定会执行。