我是个新手,所以如果这听起来很天真,请原谅我。我用fastcgi++写了一个脚本。我测试了基本的用例。但是,就像一个优秀的软件工程师,我想要测试脚本,每次我做一个改变,以确保我不会破坏的东西。
我以前就是这么做的:
这是我的目录结构:
script:
- bin
- build (contained the bash script to compile the script)
- src
- tests
- build (contained bash script to compile the test)
- src (contained the test file)
- output我黑了我测试的方式。我过去经常使用curl调用我的脚本,并将其输出重定向到测试/输出中的文件(使用相对路径),并将其与预期的输出进行比较。我可以这样做,因为测试是手工编译的,并且只有在将目录更改为tests/build之后才执行测试。我最近决定使用一个构建系统。我选择了介子。使用介子进行测试的方法是运行meson test或ninja test。问题是,现在我无法控制测试的运行位置。
在这种情况下如何进行测试?你是如何测试你的fcgi脚本的?
编辑:,这是我编译和测试的一个例子。这是一个完整的可验证示例:
#include <fastcgi++/request.hpp>
#include <fastcgi++/manager.hpp>
class test : public Fastcgipp::Request<char>
{
bool response() {
nlohmann::json output;
out << "Content-Type: application/json; charset:utf-8\r\n\r\n";
out << "{\"success\": true}";
}
}
int main() {
Fastcgipp::Manager<test> manager;
manager.setupSignals();
manager.listen();
manager.start();
manager.join();
} 你可以认为回应是主要的。这就是你开始处理事情的地方。你可以拿投入,输出和所有的好东西。
我就是这样测试的:
TEST(test, test1) {
std::string fileName = "test.txt";
nlohmann::json input, output;
input["success"] = true;
std::system(std::string("curl -X GET \"localhost/cgi-bin/test.fcg\" > " + fileName).c_str());
std::ifstream file(fileName);
std::string out;
std::getline(file, out);
output = nlohmann::json::parse(out);
ASSERT_EQ(input, output);
std::system(std::string("rm " + fileName).c_str());
}注释: nlohmann::json是一个json解析器,我在测试中使用测试。
https://stackoverflow.com/questions/58129656
复制相似问题