1 概述
CppTest是一个可移植、功能强大但简单的单元测试框架,用于处理C++中的自动化测试。重点在于可用性和可扩展性。支持多种输出格式,并且可以轻松添加新的输出格式。
CppTest下载地址Sourceforge
Github地址
下面编写实际测试用例,来熟练CppTest库使用。
2 实例
2.1 接口
该接口是对libssh库C++封装,管理shh连接会话,定义如下:
namespace ssh
{
class Session
{
public:
Session();
~Session();
void set_host(const char* host);
void set_port(int port);
void set_port(const char* port);
void set_user(const char* user);
bool connect(int timeout = 10);
void disconnect();
bool verify();
AuthMethod auth_method(const char *username);
bool login(const char* password);
bool login(const char* user, const char* password);
bool login_by_interactive(const char* user, const char* password);
bool login_by_prikey(const char* user, const char* privkeyfile);
bool login_by_pubkey(const char* user, const char* pubkeyfile);
const char* error();
};
}
2.2 测试用例
2.2.1 用例定义
class SeesionSuite : public Test::Suite
{
ssh::Session sessioin_;
std::string privkeyfile_ = "d:/id_rsa";
int timeout = 5;
public:
SeesionSuite()
{
TEST_ADD(SeesionSuite::connect_without_host);
TEST_ADD(SeesionSuite::connect_error_host);
TEST_ADD(SeesionSuite::connect_not_unconnected);
TEST_ADD(SeesionSuite::connect_invalid_host);
TEST_ADD(SeesionSuite::connect_without_port);
TEST_ADD(SeesionSuite::connect_error_port);
TEST_ADD(SeesionSuite::connect_correct_port);
TEST_ADD(SeesionSuite::disconnect);
TEST_ADD(SeesionSuite::login);
}
private:
void connect_without_host();
void connect_error_host();
void connect_not_unconnected();
void connect_invalid_host();
void connect_without_port();
void connect_error_port();
void connect_correct_port();
void disconnect();
void login();
void error();
};
2.2.1 用例实现
#include "SessionTest.h"
#include <iostream>
void SeesionSuite::connect_without_host()
{
TEST_ASSERT_EQUALS_MSG(false, sessioin_.connect(timeout), "not set host, cannot connected")
error();
}
void SeesionSuite::connect_error_host()
{
sessioin_.set_host("127.0.0.1");
TEST_ASSERT_EQUALS_MSG(false, sessioin_.connect(timeout), "error host, cannot connected")
error();
}
void SeesionSuite::connect_not_unconnected()
{
sessioin_.set_host("20.20.20.109");
TEST_ASSERT_EQUALS_MSG(false, sessioin_.connect(timeout), "not unconnected, cannot connected")
error();
}
void SeesionSuite::connect_invalid_host()
{
sessioin_.set_host("x.y.z.d");
sessioin_.disconnect();
TEST_ASSERT_EQUALS_MSG(false, sessioin_.connect(timeout), "error host, cannot connected")
error();
}
void SeesionSuite::connect_without_port()
{
sessioin_.set_host("20.20.20.109");
sessioin_.disconnect();
TEST_ASSERT_EQUALS_MSG(true, sessioin_.connect(timeout), sessioin_.error())
}
void SeesionSuite::connect_error_port()
{
sessioin_.set_port(23);
sessioin_.disconnect();
TEST_ASSERT_EQUALS_MSG(false, sessioin_.connect(timeout), sessioin_.error())
std::cerr << sessioin_.error() << ":23" << std::endl;
}
void SeesionSuite::connect_correct_port()
{
sessioin_.set_port(22);
sessioin_.disconnect();
TEST_ASSERT_EQUALS_MSG(true, sessioin_.connect(timeout), sessioin_.error())
}
void SeesionSuite::disconnect()
{
sessioin_.disconnect();
}
void SeesionSuite::login()
{
sessioin_.set_host("20.20.20.109");
sessioin_.set_port(22);
TEST_ASSERT_EQUALS_MSG(true, sessioin_.connect(timeout), sessioin_.error())
TEST_ASSERT_EQUALS_MSG(true, sessioin_.verify(), "verify is failed")
ssh::AuthMethod method = sessioin_.auth_method("james");
TEST_ASSERT_EQUALS_MSG(false, ssh::isUnknowOfAuthMethod(method), "Unknow auth method!")
if(ssh::isPubKeyOfAuthMethod(method) && !privkeyfile_.empty())
TEST_ASSERT_EQUALS_MSG(true, sessioin_.login_by_prikey("james", privkeyfile_.c_str()), "Pubkey's login is failed!")
else if(ssh::isPasswordOfAuthMethod(method))
TEST_ASSERT_EQUALS_MSG(true, sessioin_.login("james", "james010"), "Password's login is failed!")
else if(ssh::isInteractiveOfAuthMethod(method))
TEST_ASSERT_EQUALS_MSG(true, sessioin_.login_by_interactive("james", "james010"), "Interactive's login is failed!")
}
void SeesionSuite::error()
{
std::cerr << sessioin_.error() << std::endl;
}
说明:文章来源:https://www.toymoban.com/news/detail-861198.html
- TEST_ASSERT_EQUALS_MSG(expected, got, msg)是CppTest库定义的宏,如果预期值expected与实际值got不同则报错,错误信息为msg
3 运行用例
3.1 主函数
int main(int argc, char *argv[])
{
Test::Suite mainSuite;
Test::TextOutput output(Test::TextOutput::Verbose);
mainSuite.add(std::unique_ptr<Test::Suite>(new SeesionSuite));
mainSuite.run(output, true);
return 0;
}
3.1 运行结果
Hostname required
Timeout connecting to 127.0.0.1
ssh_socket_connect called on socket not unconnected
Failed to resolve hostname x.y.z.d ()
Timeout connecting to 20.20.20.109:23
SeesionSuite: 9/9, 100% correct in 10.094000 seconds
Total: 9 tests, 100% correct in 10.094000 seconds
说明:文章来源地址https://www.toymoban.com/news/detail-861198.html
- 前5行是测试程序的错误打印
- 后2行是测试结果,SeesionSuite的9个测试用例都通过了。
到了这里,关于CPPTest实战演示(CppTest libssh)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!