cunit测试空隙功能

发布于 2025-01-20 09:09:56 字数 696 浏览 2 评论 0原文

您好,我是 Cunit 测试和一般测试的新手。我找不到测试这个 void 函数的方法。想知道如何正确地做到这一点或如何重构它。 关于如何正确测试 void 函数有什么建议吗?谢谢。

void validate_arg(int argc, char *argv[], struct Statistics *stats) {
   if (argc == 4 && strcmp(argv[2], "-G") == 0) {
      FILE *file = fopen(argv[3], "w");
      stop_prog(write_stats(file, stats), err1);
      fclose(file);
   }
   free(stats);
}

我编写了 Cunit 测试,但我无法断言任何内容,因为它是空的,并且我无法测试要写入的文件是否已创建,因为它在 if 内被释放。

void test_validate_args_0(void) {
   struct Stats *stats = malloc(sizeof(struct Stats));  
   char* argv[] = {"TEST", "dummy", "-G", NULL};
   CU_ASSERT_EQUAL(validate_arg(4, argv, stats), 0);  
   free(stats);  
}  

Hi Im new to Cunit tests and testing in general. I cant find a way to test this void functon. Wondering how to properly do it or how to refactor it.
Any tips on how to properly test void functions? Thanks.

void validate_arg(int argc, char *argv[], struct Statistics *stats) {
   if (argc == 4 && strcmp(argv[2], "-G") == 0) {
      FILE *file = fopen(argv[3], "w");
      stop_prog(write_stats(file, stats), err1);
      fclose(file);
   }
   free(stats);
}

The Cunit test I wrote but I cant assert anything since its a void and I cant test if my file to write was created since since it gets deallocated inside the if.

void test_validate_args_0(void) {
   struct Stats *stats = malloc(sizeof(struct Stats));  
   char* argv[] = {"TEST", "dummy", "-G", NULL};
   CU_ASSERT_EQUAL(validate_arg(4, argv, stats), 0);  
   free(stats);  
}  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文