我正在使用Mocha和Supertest编写针对REST API的测试。 我有多个环境,并且某些数据因环境而异。例如,如果我选择暂存环境,则数据与qa环境不同。我特别想在命令行中选择环境。
请检查以下测试。
const request = require("supertest");
const express = require("express");
const expect = require("chai").expect;
const flush = require('flush-cache');
var Token;
var AddID;
var stage ="http://www.stage-xyz.com";
var qa = "http://www.qa-xyz.com";
var methods = {
"login":"/login?",
"address":"/address"
}
var login = {
"user":"abc@abc.com",
"password":"xyz"
}
var add = {
"no": "121",
"streetaddress": "covey street",
"postalcode":"12345",
"province":"NY"
}
beforeEach(function () {
flush();
});
describe('POST /Authenticate', function() {
it('Retrieve Token', function(done) {
request(qa).post(methods.login)
.set('Content-Type','application/json')
.send(login)
.expect('Content-Type', /json/)
.expect(200)
.end(function(err,res) {
if (err) return done(err);
Token = res.body.token;
console.log(res.body);
expect(res).to.have.property('status').equals(200);
done();
});
});
it('address', function(done) {
request(qa).post(methods.perm)
.set('Authentication',Token)
.set('Content-Type','application/json')
.send(add)
.expect('Content-Type',/json/)
.expect(200)
.end(function(err,res) {
if (err) return done(err);
console.log(res.body);
AddID = res.body.id;
expect(res).to.have.property('status').equals(200);
expect(true).equals(Number.isInteger(AddID));
done();
});
});
});
但是,最终我想在一个名为config的单独文件中提供如下所示的输入。请帮助我创建一个配置模块以传递环境url并基于此选择数据。
var env = {
stage:{
url:"http://www.stage-xyz.com"
login : {
"user":"abc@abc.com",
"password":"xyz",
}
add : {
"no": "121",
"streetaddress": "covey street",
"postalcode":"12345",
"province":"NY"
}
}
qa:{
url:"http://www.qa-xyz.com""
login : {
"user":"abc@abc.com",
"password":"xyz"
}
add : {
"no": "121",
"streetaddress": "covey street",
"postalcode":"12345",
"province":"NY"
}
}
}
答案 0 :(得分:0)
在我的项目中,我使用config为我的环境定义配置。它真的很好。
我的项目文件夹:
int inputSize = 4;
int forwardDiagonal = inputSize*2 - 1;
for (int y = 0; y < inputSize*2; y++) {
for (int x = 0; x < inputSize*2; x++) {
if (y - x == 0) {
System.out.print("X");
} else if (y + x == forwardDiagonal) {
System.out.print("X");
} else {
System.out.print(" ");
}
}
System.out.println();
}
您也可以使用json格式。
要使其正常工作,还必须更改源文件以使用config中定义的值
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
public class Survey {
public static void main(String[] args) {
List<String> questions = Arrays.asList("Are you happy or sad today?", "Are you short or tall?", "Are you strong or weak");
List<Set<String> > answers = Arrays.asList(new HashSet<String>(Arrays.asList("happy", "sad")),
new HashSet<String>(Arrays.asList("short", "tall")),
new HashSet<String>(Arrays.asList("strong", "weak"))
);
List<String> replies = new ArrayList<String>();
Map<String, String> conclusions = new TreeMap<String, String>();
conclusions.put("[happy, short, weak]", "You are a short happy person who is weak: I suggest more exercise! ");
conclusions.put("[sad, tall, strong]", "You are a sad strong person who is tall: I suggest hugging a tree!");
conclusions.put("[sad, short, weak]", "You are a sad short person who is weak: I am sorry to hear that");
Scanner scan = new Scanner(System.in);
do {
int questionNo = replies.size();
System.out.println(questions.get(questionNo));
String reply = scan.nextLine().toLowerCase(); //Waits for input
if(answers.get(questionNo).contains(reply))
replies.add(reply);
} while (replies.size() < questions.size());
String conclusion = conclusions.get(replies.toString());
if (conclusion != null){
System.out.println(conclusion);
}
scan.close();
}
}
要通过Mocha运行测试文件,例如定位登台环境
- config
- default.yaml
- staging.yaml
- qa.yaml
- test
希望有帮助