csoltenborn / GoogleTestAdapter

Visual studio extension that adds support for the C++ testing framework Google Test.
Other
143 stars 100 forks source link

Filter by traits using --gtest_filter #313

Closed L4ZZA closed 4 years ago

L4ZZA commented 4 years ago

I can't seem to only run tests, categorised as UnitTest by a given trait, by specifying the --gtest_filter=UnitTest

Code

#include "pch.h"
#include "GTA_Traits_1.8.0.h"

#define MY_TESTATTRIBUTE                                    Type
#define MY_TESTCATEGORY_UNIT_TEST       UnitTest

#define MY_UNIT_TEST_METHOD(classname, testname)\
    TEST_TRAITS(classname, testname, MY_TESTATTRIBUTE, MY_TESTCATEGORY_UNIT_TEST)

MY_UNIT_TEST_METHOD(TestCaseName, TestName) {
  EXPECT_EQ(1, 1);
  EXPECT_TRUE(true);
}

MY_UNIT_TEST_METHOD(TestCaseName, aaaaaaaaaa) {
  EXPECT_EQ(1, 1);
  EXPECT_TRUE(true);
}

TEST(TestCaseName, cccccc) {
  EXPECT_EQ(1, 1);
  EXPECT_TRUE(true);
}

Result

C:\dev\investigations\google_test_example_nuget\bin>google_test_example_nuget.exe --gtest_filter=UnitTest
Running main() from e:\a\_work\2100\s\thirdparty\googletest\googletest\src\gtest_main.cc
Note: Google Test filter = UnitTest
[==========] Running 0 tests from 0 test cases.
[==========] 0 tests from 0 test cases ran. (1 ms total)
[  PASSED  ] 0 tests.
csoltenborn commented 4 years ago

That won't work ;-)

Traits support is a Visual Studio specific feature. The trait macros you are using encode the traits information into the gtest executable in a certain way; however, gtest doesn't know anything about this, and thus the traits are completely useless when running gtest executables from the console.

However, you can run your executables with the help of VsTest.Console.exe and Google Test Adapter, and can then filter the tests to be run by means of the traits. For a working example open and build the GTA solution, place a breakpoint at this line, and run test SampleTests_Settings_Type_EQ_Small - you will see how to invoke VsTestConsole.exe for the sake of filtering with traits.

Let me know whether this helps...

L4ZZA commented 4 years ago

The solution fails to build and I don't want to investigate that too. My problem is that we have thousands of tests scattered in different test assemblies some of which we've found out are not actual unit tests, so I wanted to tag all unit tests as such and only run those using the filter for the traits. Now, knowing that traits are only an option using vstest.console and that vstest.console is not working as expected, I don't know how can I achieve this without having to manually move each test to a different project (i.e unitTest_cpp.exe -> otherTest_cpp.exe)

csoltenborn commented 4 years ago

See Wiki for build instructions - shouldn't be too hard. However, here's the resulting command line on my machine:

VsTestConsole.exe /Logger:Trx /TestAdapterPath:C:\Users\chris\git\GoogleTestAdapter\out\binaries\GoogleTestAdapter\Debug\Packaging.GTA "..\..\..\..\..\out\binaries\SampleTests\Debug\Tests_gta.exe" /TestCaseFilter:"Type=Small"

Packaging.GTA folder is the folder where GTA is located, Tests_gta.exe is the test executable compiled from the code at SampleTests/Tests.

Not sure what you mean by "vstest.console is not working as expected"...

May traits by regexes be an option?

csoltenborn commented 4 years ago

I'm closing this due to inactivity. Feel free to add more questions.