Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refine test data in unit tests and add integration tests #12

Merged
merged 19 commits into from
Sep 19, 2024

Conversation

leoadonia
Copy link
Collaborator

改动:

  1. 将 unit test 中的 test_data 重命名为 test_data_embed, 其中维护的文件只是方便 unit test 在编译期引用.
  2. GN rust_test template 支持编译和拷贝 integration test.
  3. 将 graph check 部分测试代码引入 tests, 作为 integration test.

背景:

rust 的测试分为 unit test 和 integration test. 其中:

  • unit test 是维护在 src 目录下各自的 mod 中,测试对象是单个的 private 或 pub 方法.
  • integration test 是维护在 tests 目录下,测试对象是 crate 中的 pub 方法.

同时,运行测试用例的需求包括:

  • 在命令行中执行 cargo test.
  • 在 vscode 通过 Run Test 按钮执行.
  • 在命令行中执行 cargo build --tests, 然后将编译后的可执行文件拷贝至其他目录下执行.

目前在测试用例中存在对本地文件的访问需求,主要是两个:

  • 为了方便维护格式化文件,将 json 文件单独维护,在测试用例中通过 include_str! 的方式引用. 该方式是编译期访问的, 访问的方式是基于 .rs 文件所在目录为基准目录.
  • 在运行时,通过 fs 访问本地文件. 对于上述三种运行测试用例的方式,前两种是相对于 crate 根目录为基准目录; 第三种是相对于可执行文件所在目录为基准目录.

基于上述情况,约定如下:

  • 对于 unit test 中使用的文件,维护在各个 mod 下的 test_data_embed 目录下.
  • 在 unit test 中只能通过 include_str! 的方式引用文件.
  • 在 integration test 中,维护在 tests/test_data 目录下, 按各个测试用例名称区分.
  • 对于通过 cargo build --tests 的方式构建,并拷贝测试输出物的场景,只会涉及拷贝 tests/test_data 目录.

@leoadonia leoadonia requested a review from halajohn as a code owner September 12, 2024 12:39
@halajohn halajohn self-assigned this Sep 12, 2024
@leoadonia leoadonia changed the title Refine test data in unit tests and add integration tests. fix: refine test data in unit tests and add integration tests. Sep 13, 2024
@leoadonia leoadonia changed the title fix: refine test data in unit tests and add integration tests. fix: refine test data in unit tests and add integration tests Sep 13, 2024
@leoadonia leoadonia force-pushed the feature/refine-rust-test branch 2 times, most recently from 23795a8 to fff3310 Compare September 13, 2024 09:50
@leoadonia leoadonia force-pushed the feature/refine-rust-test branch from fff3310 to b0d018a Compare September 13, 2024 10:31
@halajohn halajohn merged commit b0eca9c into main Sep 19, 2024
15 checks passed
@halajohn halajohn deleted the feature/refine-rust-test branch September 19, 2024 03:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants