Skip to content

Run all tests without arguments #2028

@senyai

Description

@senyai

Hi! I find it confusing that the tests require arguments. I want to change it.

Here are my reasons:

  1. Not all tests check for required arguments
    bool found = false;
    for (int i = 0; i < argc; ++i) {
    if (strcmp("--benchmark_filter=BM_NotChosen", argv[i]) == 0) {
    found = true;
    break;
    }
    }
    assert(found);
    and so it's easy to run a test incorrectly.
  2. There are GitHub issues mentioning that a test fails when the actual problem was that the correct arguments were not passed.
  3. Arguments need to be duplicated between test/CMakeLists.txt and test/BUILD and they are actually not in sync now (perf_counters_test is run with different arguments).

My solution:

  1. Create test/default_arguments.h:
// ...

void AddTestArguments(int &argc, char **&argv, std::initializer_list<char const*> args={}) {
  if (argc > 1) {
    std::cout << "Warning: User is not expected to pass any command line arguments\n";
  }
  static std::vector<char const*> new_argv;
  new_argv.insert(new_argv.end(), argv, argv + argc);
  new_argv.insert(new_argv.end(), args.begin(), args.end());
  new_argv.push_back("--benchmark_min_time=0.01s");
  argv = const_cast<char**>(new_argv.data());
  argc = static_cast<int>(new_argv.size());
}
  1. Call it after each main function like this
int main(int argc, char* argv[]) {
  AddTestArguments(argc, argv, {"--benchmark_counters_tabular=true"});
  benchmark::MaybeReenterWithoutASLR(argc, argv);
  RunOutputTests(argc, argv);
}
  1. Cleanup the code

Sounds good?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions