简体   繁体   English

google test 是如何制作测试序列的

[英]How does google test make test sequence

How google-test makes test sequence (or order of test case execution) for testing test cases? google-test 如何制作测试序列(or order of test case execution)来测试测试用例?

Suppose I have 5 test cases.假设我有 5 个测试用例。

TEST(First, first)
TEST(Secnd, secnd)
TEST(Third, third)
...
TEST(Fifth, fifth)

How google-test test above test cases? google-test如何测试上面的测试用例? I mean in what sequence?我的意思是按什么顺序? Or can we provide any test sequence?或者我们可以提供任何测试序列吗?

By default it will test them in the order it finds them at link time, which will depend upon your tools.默认情况下,它将按照在链接时找到它们的顺序测试它们,这取决于您的工具。

You can select which tests to run , such as a subset, or a single test.您可以选择要运行的测试,例如子集或单个测试。

There is also an option to run them in a random order .还可以选择以随机顺序运行它们

The advanced reference pages for googletest in the chapter Shuffling the Tests tells :改组测试一章中 googletest 的高级参考页面告诉:

By default, Google Test uses a random seed calculated from the current time.默认情况下,Google 测试使用从当前时间计算的随机种子。 Therefore you'll get a different order every time.因此,您每次都会收到不同的订单。

This is actually a good way of unit testing, since tests should not depend on the order of execution.这实际上是一种很好的单元测试方式,因为测试不应该依赖于执行的顺序。

As far as I know, there are no ways of setting the order of tests execution.据我所知,没有办法设置测试执行的顺序。 The only parameter you can set is the seed, used to set the same order of execution.您可以设置的唯一参数是种子,用于设置相同的执行顺序。

By default they run in the declaration order. 默认情况下,它们按声明顺序运行。 As said by other, you have to provide the flag --gtest_shuffle to shuffle them.正如其他人所说,您必须提供标志--gtest_shuffle来随机播放它们。

Even if you can guess some pattern for the order of execution (as written, or linked) you shouldn't depend on that.即使您可以猜测执行顺序的某种模式(如书面或链接),您也不应该依赖于此。

It would be repeated on different executions, though.但是,它会在不同的执行中重复。 If you don't want it to happen, you can use --gtest-shuffle .如果您不希望它发生,您可以使用--gtest-shuffle That runs the test on a random order, according to a random seed.根据随机种子,以随机顺序运行测试。

In case of failure, you can use --gtest_random_seed= with that number and repeat the exact sequence (to investigate why it failed).如果失败,您可以使用--gtest_random_seed=与该数字并重复确切的序列(以调查失败的原因)。

That said, the randomness is not complete:也就是说,随机性是不完整的:

  • Test suites will be run in a random order测试套件将以随机顺序运行
  • Tests within a test suite will be run in a random order测试套件中的测试将以随机顺序运行

If not run in this way, SetUpTestSuite and TearDownTestSuite methods would be mixed.如果不以这种方式运行,SetUpTestSuite 和 TearDownTestSuite 方法将混合使用。 You don't need a fixture for this grouping to happen, though.但是,您不需要固定装置来进行这种分组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM