好吧,我对测试很陌生,但我想将编程提升到一个新的水平并开始测试,没有单元测试,而是高级测试。例如:插入一个作业并通过检查它的输入和结果来检查它是否有效。或者另一个例子:插入一个工作并且工人 X 启动它:现在我将检查工作和工人的属性,以了解它们是否处于有效状态。
但这非常复杂,所以我一直在考虑在每个测试中做一个环境和一个实例,每个环境创建一个服务器实例并分配一个空数据库来使用;
例如 start job 高级案例:
var environment = new GenericEnvironment(test); // test will be used to throw errors within the environment when something goes wrong.
environment.insertWorker({name: 'bla bla', number 99});
environment.insertARandomProduct();
environment.insertJob({productId: product._id})
environment.startJob({workerId: worker._id})
var environmentValidator = new environmentValidators.StartJobEV()
var job = environment.getLastInsertedJob(environment, test);
environment.validate(job);
// node.js (Javascript on server)
这个例子非常简单,因为在现实世界中它使用异步回调。
environmentValidator 使用来自环境的数据来检查作业是否处于有效状态。例如,还要让正在工作的工作人员检查它的状态是否很忙。
但我不认为这很好,因为我正处于创建测试的开始阶段,所以我问你是否有可能改进进行这些高级测试的方式。
另外我想知道这些测试是否有任何现有的框架?
谢谢
真实世界的代码,但仅用于在插入作业后检查状态:
var serverManager = new ServerManager(test.getPathForDatabase());
require('production/tests/tottys/_environments/DefaultEnvironment');
var environment = new production.tests.tottys._environments.DefaultEnvironment(test, serverManager);
var dummyData = {}
test.addStep('initEnvironment', function(nextStep){
environment.init();
environment.addListener('afterInitialized', function(){
nextStep();
}, this);
});
test.addStep('insertWorker', function(nextStep){
dummyData.worker = environment.insertRandomWorker(function(data){
nextStep();
});
});
test.addStep('insertProduct', function(nextStep){
dummyData.product = environment.insertRandomProduct(function(data){
nextStep();
});
});
test.addStep('insertJob', function(nextStep){
var worker = environment.getLastInsertedWorker();
var product = environment.getLastInsertedProduct();
dummyData.job = environment.insertRandomJob(worker, product, function(data){
nextStep();
});
});
test.addStep('updateWorker', function(nextStep){
environment.updateWorker({workerId: environment.getLastInsertedWorker()._id}, function(data){
nextStep();
});
});
test.addStep('validateInsertedJob', function(nextStep, test){
require('production/tests/tottys/_environmentValidators/AfterJobInserted');
var EV = new production.tests.tottys._environmentValidators.AfterJobInserted(environment, test);
var job = environment.getLastInsertedJob();
EV.addListener('validationEnded', function(e){
nextStep();
});
EV.validate(job, dummyData);
});
test.start();
和验证文件:
qx.Class.define("production.tests.tottys._environmentValidators.AfterJobInserted", {
extend: qx.core.Object
,properties: {
}
,events: {
validationEnded: 'qx.event.type.Data'
}
,construct: function(environment, test){
this.__environment = environment;
this.__test = test;
}
,members: {
validate: function(job, dummyData){
this.__job = job;
this.__dummyData = dummyData;
this.__environment.getWorkerForJob(job, this.__afterWorkerReturned, this);
this.__environment.getProductForJob(job, this.__afterProductReturned, this);
}
,__afterWorkerReturned: function(worker){
this.__worker = worker;
this.__tryStartValidation();
}
,__afterProductReturned: function(product){
this.__product = product;
this.__tryStartValidation();
}
,__tryStartValidation: function(){
if(this.__preConditionsAreValid()){
this.__startValidation();
}
}
,__preConditionsAreValid: function(){
if(this.__worker && this.__product){
return true;
}
return false;
}
,__startValidation: function(){
var test = this.__test;
var dummyData = this.__dummyData;
var job = this.__job;
var worker = this.__worker;
var product = this.__product;
test.add("Expect job not to be done.", function(expect){
expect(job.done).toBe(false);
})
test.add("Expect job's requested quantity to be the same as requested.", function(expect){
expect(job.qtdRequested).toBe(dummyData.job.qtdRequested);
})
test.add("Expect job's missing quantity be the same as requested.", function(expect){
expect(job.qtdMissing).toBe(dummyData.job.qtdRequested);
})
test.add("Expect to be requested by the same request type.", function(expect){
expect(job.requestedByType).toBe('manager')
})
test.add("Expect job not to be done.", function(expect){
expect(job.done).toBe(false);
})
test.add("Expect job's done quantity to be '0' (Number)", function(expect){
expect(job.qtdDone).toBe(0);
})
test.add("Expect job to not have any time frames.", function(expect){
expect(job.timeFrames && job.timeFrames.length > 0).notToBe(true);
})
test.add("Expect job's date end to not exist.", function(expect){
expect(job.dateJobEnd).notToExist();
})
test.add("Expect job's date request to exist.", function(expect){
expect(job.dateRequested).toExist();
})
test.add("Expect job's state to be 'todo'.", function(expect){
expect(job.state).toBe('todo');
})
test.add("Expect job's order to be '0' (Number).", function(expect){
expect(job.order).toBe(0);
})
test.add("Expect job's worker's name to be the same as the requested one.", function(expect){
expect(job.forWorker.name).toBe(dummyData.worker.name);
})
test.add("Expect job's worker's number to be the same as the requested one.", function(expect){
expect(job.forWorker.number).toBe(dummyData.worker.number);
})
test.add("Expect job's worker's _id to be the same as the generated one.", function(expect){
console.log(worker);
expect(job.forWorker._id.toString()).toBe(worker._id.toString());
})
test.add("Expect job's product's code to be the same as the requested one.", function(expect){
expect(job.product.code).toBe(dummyData.product.code);
})
test.add("Expect job's product's description to be the same as the requested one.", function(expect){
expect(job.product.description).toBe(dummyData.product.description);
})
test.add("Expect job's product's _id to be the same as the requested one.", function(expect){
expect(job.product._id.toString()).toBe(product._id.toString());
})
this.fireDataEvent('validationEnded', {err: false})
}
}
});