知识卡片
并行测试隔离共享状态
同一测试二进制中的测试默认并行。若多个用例共写 test-output.txt,一个用例可能在另一个用例读取前改写文件。优先让用例使用独立文件和状态。需要串行时用 cargo test -- --test-threads=1,代价是反馈更慢。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 编写自动化测试 Controlling How Tests Are Run · 查看原书依据
For example, say each of your tests runs some code that creates a file on disk named test-output.txt and writes some data to that file. Then each test reads the data in that file and asserts that the file contains a particular value, which is different in each test.
参考来源
- 位置:《The Rust Programming Language(第2版)》第11章 编写自动化测试(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c11.xhtml`,块 0142)
- 结论依据:共享文件例子说明测试之间可因读写交错导致与实现无关的失败。
- 原始内容:For example, say each of your tests runs some code that creates a file on disk named test-output.txt and writes some data to that file. Then each test reads the data in that file and asserts that the file contains a particular value, which is different in each test.