如何使这些测试算代码不错

how to make these testcases code nice?

本文关键字:代码 测试 何使这      更新时间:2023-10-16

我是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
}

和这样的测试(使用Google测试)

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);
}

我认为您正在寻找的是固定装置。我不熟悉Google测试,但是在大多数测试框架中都应使用测试固定装置。您应该研究该文档。