Javascript 摩卡动态测试生成与动态数据生成
我创建了一个名为Javascript 摩卡动态测试生成与动态数据生成,javascript,mocha.js,integration-testing,chai,supertest,Javascript,Mocha.js,Integration Testing,Chai,Supertest,我创建了一个名为testLoginFailed 让testloginFailed=(应用程序、标题、数据)=>{ it(标题、功能(完成){ 请求(应用程序) .post(apidemps.auth.login) .发送(数据) 。然后((响应)=>{ 响应状态码应等于(401); response.body.error.name.should.equal('error'); response.body.error.message.should.equal('login failed'); re
testLoginFailed
让testloginFailed=(应用程序、标题、数据)=>{
it(标题、功能(完成){
请求(应用程序)
.post(apidemps.auth.login)
.发送(数据)
。然后((响应)=>{
响应状态码应等于(401);
response.body.error.name.should.equal('error');
response.body.error.message.should.equal('login failed');
response.body.error.code.should.equal('LOGIN_FAILED');
完成();
})
.catch((错误)=>{
完成(错误);
});
});
};
这是我的描述块
describe('login negative Tests', () => {
before(function () {
let loginFailedTests = [
{
title: 'it should fail user login using mobile because of incorrect mobile',
data: {
username: '1223334444',
password: options.user.password
}
}, {
title: 'it should fail user login using mobile because of incorrect password',
data: {
username: options.user.mobileNumber,
password: options.user.password + '123'
}
}
];
});
loginFailedTests.forEach((test) => {
testloginFailed(app, test.title, test.data);
});
});
问题陈述:
descripe()
及其回调将在before
挂钩之前执行。执行顺序是descripe()
=>before()
=>it()
因此,您应该将测试数据的初始化过程放在descripe()
块中
例如
const{expect}=require('chai');
const options={user:{password:'123',mobileNumber:'321'};
常量app={};
让testloginFailed=(应用程序、标题、数据)=>{
it(标题、功能(){
期望(1+1).to.be.eql(2);
});
};
描述('登录负面测试',()=>{
让loginFailedTests=[
{
标题:“由于手机不正确,用户使用手机登录失败”,
数据:{
用户名:“1223334444”,
密码:options.user.password,
},
},
{
标题:“由于密码不正确,用户使用手机登录失败”,
数据:{
用户名:options.user.mobileNumber,
密码:options.user.password+'123',
},
},
];
登录失败测试forEach((测试)=>{
testloginFailed(应用程序、test.title、test.data);
});
});
测试结果:
login negative Tests
✓ it should fail user login using mobile because of incorrect mobile
✓ it should fail user login using mobile because of incorrect password
2 passing (5ms)
尝试在before块外部创建变量,并在before块内部设置它的值。@pulkitsinghal不起作用,因为在before块外部声明loginFailedTests会给循环提供一个空数组,因此不会生成测试
login negative Tests
✓ it should fail user login using mobile because of incorrect mobile
✓ it should fail user login using mobile because of incorrect password
2 passing (5ms)