JohnnyHendriks / TestAdapter_Catch2

Visual Studio Test Adapter for Catch2
MIT License
105 stars 28 forks source link

Cannot discover tests in VS2022 #71

Closed RobertBernstein closed 11 months ago

RobertBernstein commented 11 months ago

I have tried a number of solutions to my problem (including from StackOverflow and other closed issues here) and would really appreciate some help getting the Catch2 VS Test Adapter to discover tests in my solution/project. I am using .vcxproj and .sln files, not CMake. I have done the following:

  1. Disabled the Google Test and Boost Test adapter extensions.
  2. Disabled a number of other VS extensions.
  3. Eliminated all other errors in the Test output window.
  4. Selected Minimal.runsettings to be my Solution Wide runsettings file.
  5. Disabled Auto detect runsettings files.

I still have no tests discovered in my Catch2Test.exe file. How do I fix this?

At one point, I included std::cout << Factorial(6) << '\n'; in my main method and I saw 720 in the output from the test adapter discovery process, which was weird.

This is the entire Catch2Test.cpp file:

#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include <iostream>

unsigned int Factorial(unsigned int number) {
    return number <= 1 ? number : Factorial(number - 1) * number;
}

TEST_CASE("Factorials are computed", "[factorial]") {
    REQUIRE(Factorial(0) == 1);
    REQUIRE(Factorial(1) == 1);
    REQUIRE(Factorial(2) == 2);
    REQUIRE(Factorial(3) == 6);
    REQUIRE(Factorial(10) == 3628800);
}

int main()
{
}

and this is the entire Minimal.runsettings file:

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>

    <Catch2Adapter>
        <FilenameFilter>.*</FilenameFilter>
        <Logging>debug</Logging>
    </Catch2Adapter>

</RunSettings>

I get the following output:

========== Starting test discovery ==========
Started Catch2Adapter test discovery...
Discover log:
Source: D:\Dev\GitHub\RobertBernstein\Catch2Test\x64\Debug\Catch2Test.exe
CheckSource name: Catch2Test
  No output
  Default Verbose Test Discovery:
  Testcase count: 0
  Accumulated Testcase count: 0

Start adding test cases to discovery sink
Finished adding test cases to discovery sink
Finished Catch2Adapter test discovery.
No test is available in D:\Dev\GitHub\RobertBernstein\Catch2Test\x64\Debug\Catch2Test.exe. Make sure that test discoverer & executors are registered and platform & framework version settings are appropriate and try again.
========== Test discovery finished: 0 Tests found in 584.2 ms ==========
JohnnyHendriks commented 11 months ago

Looks like you have 2 main functions. One via #define CATCH_CONFIG_MAIN (this is the one you want). and an empty one that does nothing that you define yourself (this one is blocking/masking the one you need). I expect that if you delete the one you defined yourself everything should work.

RobertBernstein commented 11 months ago

I thought that might be the problem, but when I delete it I get a linker error. I'm not at the computer right now, but it's telling me that my project has no main function. I even tried telling it I don't need a main function in the Advanced linker settings, but got an error that this setting can only be used for DLLs. Any idea how to eliminate the linker error?

RobertBernstein commented 11 months ago

When I remove the main method that I added, the error I'm getting is:

1>------ Rebuild All started: Project: Catch2Test, Configuration: Debug x64 ------
1>Catch2Test.cpp
1>MSVCRTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
1>D:\Dev\GitHub\RobertBernstein\Catch2Test\x64\Debug\Catch2Test.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "Catch2Test.vcxproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
========== Rebuild started at 8:05 PM and took 01.607 seconds ==========
JohnnyHendriks commented 11 months ago

This is not a problem with the test adapter so I'm closing this issue.

That being said, I did a quick search and the following stackoverflow question may give you a hint on what the problem may be and how to solve it: Error LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)"

RobertBernstein commented 11 months ago

@JohnnyHendriks: I finally figured it out. I included #include <catch.hpp>, but I did not add Catch2Main.lib to my Release build Additional Dependencies and Catch2Maind.lib to my Debug build Additional Dependencies in my Visual Studio C++ project. Now it works. I guess I took the whole "it's a header-only" library too literally.