0

我是 TDD 和 C++ 的新生,我写了一些代码作品,但看起来很难看......

你能给我一些关于如何重构这些代码的提示吗?

我这样定义测试用例:

// define some test case
namespace test_case_planes {
const std::string t_filename = "planes.segy_first_trace";
boost::uintmax_t t_file_size = 5888;
boost::uintmax_t t_traces_size = 1;
boost::int16_t t_trace_samples_size = 512;
boost::int16_t t_sample_interval = 4000; // 2000us, 2ms
}

namespace test_case_ld0042_file_00018 {
const std::string t_filename = "ld0042_file_00018.sgy_first_trace";
boost::uintmax_t t_file_size = 12040;
boost::uintmax_t t_traces_size = 1;
boost::int16_t t_trace_samples_size = 2050;
boost::int16_t t_sample_interval = 2000; // 2000us, 2ms
}

并像这样测试(用谷歌测试)

TEST(Segy, constructWithNoParas){
    using namespace test_case_planes;
    segy::Segy* test_segy = new segy::Segy();
    EXPECT_EQ(test_segy->getFilename(), t_filename);
}

TEST(Segy, constructWithFilename){
    using namespace test_case_ld0042_file_00018;
    segy::Segy* test_segy = new segy::Segy(t_filename);
    EXPECT_EQ(test_segy->getFilename(), t_filename);
}

TEST(Segy, setFilename){
    using namespace test_case_ld0042_file_00018;
    segy::Segy* test_segy = new segy::Segy();
    test_segy->setFilename(t_filename);
    EXPECT_EQ(test_segy->getFilename(), t_filename);
}
4

1 回答 1

1

I think what you are looking for is a fixture. I am not familiar with google test, but test fixtures should be available in most test frameworks. You should look into the documentation for that.

于 2012-11-09T15:33:32.587 回答