I'm trying to use Visual Assert for testing. Visual Studio says the method I want to test which is defined inside main.cpp is undefined in the Test Fixture.
MyFunctionTest.cpp:
#include <cfixcc.h>
class ExampleTest : public cfixcc::TestFixture
{
private:
public:
void Test()
{
CFIXCC_ASSERT_EQUALS(4, MyFunction(2,2));
}
};
CFIXCC_BEGIN_CLASS(ExampleTest)
CFIXCC_METHOD(Test)
CFIXCC_END_CLASS()
I didn't make a separate project for tests so the two files are a part of the same project. How can I have MyFunction visible for Visual Assert to work properly?
#include the header that declares MyFunction()
Related
I want to use Visual Studio's Test Explorer to run my Google Tests. When I create a console project and add a default Google Test project, and build the solution, it finds the tests as intended.
Now I want to create my own class, where everything is set up in the header file.
class foo
{
public:
foo() : the_count(0) {}
~foo() = default;
void count_plus() { the_count++; };
int get_count() { return the_count; };
private:
int the_count;
};
Then I modify my test.cpp file (the default file created by Visual Studio's Google Test project) to make use of my new class.
#include "pch.h"
#include <iostream>
#include "..\ConsoleApplication2\foo.h"
class tester : public testing::Test {
public:
foo bar;
void SetUpTestSuite() {
std::cout << "Setup..\n";
}
void TearDownTestSuite() {
std::cout << "Teardown..\n";
}
};
TEST_F(tester, TestFixture1)
{
EXPECT_EQ(bar.get_count(), 0);
bar.count_plus();
EXPECT_EQ(bar.get_count(), 1);
}
Building this solution also automatically detects the tests and runs them succesfully.
Now it gets interesting... When I move my implementation of foo to a .cpp file.
foo.h
class foo
{
public:
foo();
~foo() = default;
void count_plus();
int get_count();
private:
int the_count;
};
foo.cpp
#include "foo.h"
foo::foo()
{
the_count = 0;
}
void
foo::count_plus()
{
the_count++;
}
int
foo::get_count()
{
return the_count;
}
And then I build the solution, I initially get a linker error complaining about unresolved externals.
However, if I change the test project's linker settings to point to the other project like so:
Properties -> Linker -> Input -> Additional Dependencies -> add $(SolutionDir)ConsoleApplication2\$(IntDir)*.obj
which I got from this answer, I can succesfully build the project.
However, after I finish building the project, I'm no longer able to see or run my tests.
Am I doing something wrong here? Or is Visual Studio just broken?
I figured it out. The project which generates the object files cannot have a main() function, as this overrides Google Test's main() function.
I solved it by:
Creating an empty project (with no main()) which contains all my source code and set this project's Configuration Type as Static library (.lib).
Creating a console application project (or whatever suits your needs) which contains my main() entry point. In this project, I add a reference to my .lib project.
Create a Google Test project. In this project, I also add a reference to my .lib project.
Set the project in step two as the Startup project.
Now when you want to run your code, it will run it from your one entry point, but grab the source from .lib project and your Google Test project will also grab this .lib file, detecting all your tests.
i'm trying to build google v8 using visual studio 2019(i'm able to build using official steps with clang), and got below error:
Error C2027 use of undefined type “v8::internal::Object" v8_base_without_compiler E:\v8\src\handles\handles.h 144
V8_INLINE T operator*() const {
// unchecked_cast because we rather trust Handle<T> to contain a T than
// include all the respective -inl.h headers for SLOW_DCHECKs.
SLOW_DCHECK(IsDereferenceAllowed(INCLUDE_DEFERRED_CHECK));
return T::unchecked_cast(Object(*location())); // error on this line
}
my understanding of this part is, handles.h includes objects.h and has forward declaration for class Object (from objects.h), and inline operator method is calling Object constructor, which leads to c2027 use of undefined type v8::internal::Object
i created a simple c++ project with visual studio 2019 to reproduce the same error message
handles.h
#ifndef HANDLES_H_
#define HANDLES_H_
// forward declaration
class Object;
class Handle {
public:
__forceinline void* op() {
return new Object(0); // error here c2027 use of undefined type "Object"
}
void dummy();
};
#endif
objects.h
#ifndef OBJECTS_H_
#define OBJECTS_H_
class Object {
public:
explicit Object(int n):_n(n) {
}
void dummy();
int _n;
};
#endif
which is similar logic, an inline method implementation calls another class constructor
i don't understand how google's official steps with gn and clang could compile through the whole project, but msvc errors out
how to make it work for visual studio 2019? thanks!
I'm copying #JVApen's comment as answer
add #include "ojbects.h" will fix the issue
and if you're trying to build google's v8 using visual studio 2019, add #include "objects/objects.h" in handles/handles.h, then it will compile through
I am trying to learn to use Google Tests, so I have this template class for which I want to used typed tests.
This is the tests file that shows a warning:
#include "gtest/gtest.h"
#include "gtest/gtest-typed-test.h"
#include "../Laborator/DynamicVector.hpp"
template <typename ELEM_T>
class DynamicVectorTest : public ::testing::Test
{
public:
typedef DynamicVector<ELEM_T> Vector;
};
#if GTEST_HAS_TYPED_TEST
typedef ::testing::Types < char, int, short > Implementations;
TYPED_TEST_CASE(DynamicVectorTest, Implementations); // underlined with green
TYPED_TEST(DynamicVectorTest, Implementations)
{
TypeParam value{};
typename TestFixture::Vector vec;
EXPECT_EQ(vec.getSize(), 0);
vec.addElement(value);
EXPECT_EQ(vec.getSize(), 1);
}
#endif // GTEST_HAS_TYPED_TEST
I have successfully compiled my tests, and they all ran and passed.
When hovering over the line, visual studio says: "Function definition for 'TYPED_TEST_CASE' not found.", however when I click "Go to definition" it successfully takes me inside "gtest-typed-test.h"
Do you have any idea what I could do about this? I tried manually including the header, although it is included inside gtest-all.cc and should be visible.
Picture:
I am trying to build my project on both linux and windows. The project is already working on windows but I am facing a very weird error on GCC. Consider the following code:
Class Base {
private:
MessageInfo *createMsg();
}
Class MessageInfo {
private:
class Message{
...
}
public:
Message *messages[MAX_NO_MESSAGES];
...
}
MessageInfo *Base::createMsg(){
...
MessageInfo *newMsg = new MessageInfo;
newMsg->messages[i] = new MessageInfo::Message();
...
}
Now the problem is that in Visual Studio every thing compiles but in GCC I get the following error:
*Error: Class MessageInfo::Message is private
I am really surprised that the code actually compiles in Visual Studio but not in GCC. Any Suggestions???
EDIT:
I think I have to ask my question in a better way. My question is how is that even possible to compile such code in VS 2005??? I tried my code in VS2013 and it gave me the same error as GCC. So I am not arguing that the code is correct!
This does not compile in Visual Studio 2013:
class MessageInfo {
class Message { };
public:
Message *messages[256];
};
class Base {
MessageInfo *createMsg();
};
MessageInfo *Base::createMsg() {
MessageInfo *newMsg = new MessageInfo;
newMsg->messages[0] = new MessageInfo::Message(); // won't compile
}
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
The error, as you would expect, is:
error C2248: 'MessageInfo::Message' : cannot access private class declared in class 'MessageInfo'
Compilers are so different and I'm not really sure why VS would allow that to compile, any compiler I've used would have thrown a compile error.
What you're doing though is a bad way to think of a class. If you have the
class Message {...};
private in MessageInfo, then you shouldn't be able to access it anywhere outside of the class. We would only want Message to be available inside of the MessageInfo class and nowhere else. So you shouldn't return a Message* as you do in MessageInfo and you won't be able to access the Message() constructor as you do in main. Programming this way isn't allowed because otherwise it would completely goes against the purpose of encapsulation. If you really do want Message to be available to anyone, then make it public, but usually the inner workings of a class are hidden away from the user good reason: they shouldn't need to worry about it.
As suggested by egrunin I used the code that he suggested in his comment. Apparently you cannot compile that code in Visual Studio 2013 but I successfully compiled it on my machine using Visual Studio 2005. Here is the complete code that
used:
// Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
class MessageInfo {
class Message { };
public:
Message *messages[256];
};
class Base {
MessageInfo *createMsg();
};
MessageInfo *Base::createMsg() {
MessageInfo *newMsg = new MessageInfo;
newMsg->messages[0] = new MessageInfo::Message(); // won't compile
return newMsg;
}
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
Am I doing some thing wrong here???
P.S:
I tried the code on Visual Studio 2010 and I got the cannot access private class declared in class 'MessageInfo' error!! Can anyone compile this code on Visual Studio 2005 so that I know what is wrong with VS 2005????
I try to use MSTest so as to perform unit tests on native C++ code (so as to try to implement Test Driven Development).
There's an MSTest entry in the "Add New Project" C++ Wizard. It creates some code obviously in C+++/CLI.
However, whenever I try to run these tests, Visual Studio indicates me that the tests are not runnable:
Not Runnable TestMethod2 CargoOCRTests UTA007: Method TestMethod2 defined in class CargoOCRTests.UnitTest1 does not have correct signature. Test method marked with the [TestMethod] attribute must be non-static, public, does not return a value and should not take any parameter. for example: public void Test.Class1.Test().
However I think my two tests functions do respect the prototype VS is complaining about:
namespace CargoOCRTests
{
[TestClass]
public ref class UnitTest1
{
[TestMethod]
void TestMethod1()
{
Assert::IsTrue(true);
};
[TestMethod]
void TestMethod2()
{
Assert::IsTrue(false);
};
};
}
Have you any idea what is the cause ?
You need to mark your test methods as public
public:
[TestMethod]
void TestMethod1() {}