我有一堆接近100的测试。它们不再是单元测试,实际上它们更像是集成测试。 我想知道是否有更好的测试我的代码。 我包括了我正在执行的测试的样本。 我需要添加1秒的等待时间,因为我正在使用线程。
我每次构建都需要100秒,这似乎过分,而且添加的测试越多,性能就会越差。
TEST_F(UserInterfaceTest, DownloadConfiguration)
{
downloadconfigurationcommand = commands.DownloadConfiguration(20);
downloadconfigurationresponse = Response::GetDownloadConfigurationResponse();
EXPECT_CALL(*m_View, SetNoteBookSelection(Display::SurveyPanel));
EXPECT_CALL(*m_View, SaveFilePathDialog()).WillOnce(Return(wxID_OK));
std::string val("C:\\Temp\\version20.bin");
EXPECT_CALL(*m_View, GetSavePath()).WillOnce(Return(val));
EXPECT_CALL(*m_Param, SetFunction(DOWNLOAD_CONFIGURATION));
EXPECT_CALL(*m_Param, SetPathName(_));
EXPECT_CALL(*m_Param, SetSurveyTimes(_));
EXPECT_CALL(*m_Param, SetSurveyDelay(_));
EXPECT_CALL(*m_Param, GetFunction()).WillOnce(Return(DOWNLOAD_CONFIGURATION));
EXPECT_CALL(*m_Param, GetPathName()).WillOnce(Return(val));
EXPECT_CALL(*m_Param, GetSurveyTimes()).WillOnce(Return(1));
EXPECT_CALL(*m_Param, GetSurveyDelay()).WillOnce(Return(20));
EXPECT_CALL(*m_View, CheckInstance(_, _));
EXPECT_CALL(*m_View, GetSerialPortInstance()).WillRepeatedly(ReturnRef(serialport));
EXPECT_CALL(*m_View, GetSerialPortAddress()).WillOnce(Return(port));
EXPECT_CALL(*m_View, CustomEventDisplayData(_)).Times(AtLeast(1));
EXPECT_CALL(serialport, Read(_))
.WillOnce(DoAll(SetArgReferee<0>(downloadconfigurationresponse), Return(0)));
EXPECT_CALL(serialport, Write(_))
.WillOnce(DoAll(SetArgReferee<0>(downloadconfigurationcommand), Return(0)));
EXPECT_EQ(wxTHREAD_NO_ERROR, m_Controller->DownloadConfiguration());
Sleep(1000);
}
这就是控制器的作用。
wxThreadError wxframecontroller::DownloadConfiguration()
{
wxThreadError err = wxTHREAD_MISC_ERROR;
m_View->SetNoteBookSelection(Display::SurveyPanel);
int dialog = m_View->SaveFilePathDialog();
if (dialog == wxID_OK)
{
m_Param->SetFunction(DOWNLOAD_CONFIGURATION);
m_Param->SetPathName(m_View->GetSavePath());
m_Param->SetSurveyTimes(1);
m_Param->SetSurveyDelay(sec_10);
m_View->CheckInstance(m_View->GetSerialPortInstance(), m_View->GetSerialPortAddress());
{
CCThread* t = new CCThread(m_View, m_View->GetSerialPortInstance(), m_Param);
err = t->Create();
if (err != wxTHREAD_NO_ERROR)
{
wxMessageBox(_("Couldn't create thread!"));
delete t;
t = NULL;
}
else
{
err = t->Run();
}
if (err != wxTHREAD_NO_ERROR)
{
wxMessageBox(_("Couldn't run thread!"));
delete t;
t = NULL;
}
}
}
return err;
}
答案 0 :(得分:3)
每当您向测试添加睡眠时,您实际上是在等待其他事情发生,但未与之同步。怀疑自己在测试中的任何睡眠。
尝试编写代码,以便可以测试与线程分离的功能。如果您不能这样做,请使测试最后加入线程(这样您就可以知道您的测试拆解实际上是破坏了整个测试)。
请注意,如果计算机负载更高,当前测试会随机失败-仅此一项就表明您的测试不稳定。