Visual Studio Unit Test: Unresolved external symbol - visual-studio

I'm trying to set up a test environment using VS2015. The test project and the project to be tested both reside in the same solution and I've added a reference to the project to be tested. The code to be tested is configured as a DLL. In The object browser, things look like this, which seems to suggest that my test subjects are available to the UnitTest1 project:
Still, when I actually try to use build a simple test case I get unresolved exteral UnitTest1 symbol[...].
The actual test code is:
TEST_METHOD(TestMethod1)
{
int value = 2;
Testee t;
t.set_attribute(value);
//TODO add actual assertions
}

Related

Why am I getting a warning to include pch.h even though it's already included?

I'm trying to reproduce an issue, so I have created an empty MFC C++ application using the VS2019 wizard and a separate native Unit Test project.
Before adding the Unit Test project, the MFC application compiled and launched successfully.
The MFC application still compiles successfully, but the Unit Test project will not compile. I'm getting two errors:
E0035 #error directive: "include 'pch.h' before including this file for PCH"
C1189 #error: "include 'pch.h' before including this file for PCH"
However, the only file in the Unit Test project (UnitTest1.cpp) already includes pch.h at the top of the file:
#include "pch.h"
#include "CppUnitTest.h"
#include "../MFCApplication1/MFCApplication1.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTest1
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestMethod1)
{
CMFCApplication1App app;
bool result = app.InitInstance();
Assert::IsTrue(result);
}
};
}
It seems to be telling me to do something that is already done.
What's going on here?
Try to include stdafx.h and remove pch.h, it resolves the issue
I had the same issue. I found that I could not include the MFC DLL project's default header file directly. In your example this would be #include "../MFCApplication1/MFCApplication1.h"
I eventually found this example MFC DLL project code: https://github.com/Microsoft/VCSamples/tree/master/VC2010Samples/MFC/advanced from a list of examples here.
Notice that in the example that the default header/implementation files created by the MFC DLL project creation wizard (in this example, DLLScreenCap.h) don't export or provide any additional functionality. An existing MFC DLL project that I work with did the same.
So I added a class to the MFC DLL project to be tested and exported a simple function from it, and tested this exported function from my unit test project after linking to the project under test.
Exported class and function look like this:
#pragma once
class __declspec(dllexport) MFCLibraryExports
{
public:
// tests returning 17 to test unit test library against an mfc project
int SampleExport();
};
In your example I know that you are trying to test an instanced app and my answer doesn't help with that, but I was able to confirm that I can test at least a function exported from an MFC DLL project using the MS Unit Test framework. I'm not sure if you are expected to be able to get access to the application from a unit test as in your example; I am not able to include that header directly.

Visual Studio test project with MSTest tests reports "the project does not contain any tests"--though it does

I have a Visual Studio 2010 solution with several projects, including .NET4 test projects. One such project, when I try to run it without debugging (Ctrl+F5), reports:
"Cannot start test project MyTestProject because the project does not contain any tests."
But, the project has several tests. Each test class is marked with the [TestClass] attribute, and each test method within is marked with the [TestMethod] attribute. Just to demonstrate my own sanity, I added this file and it still wouldn't recognize that there are tests.
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Foo {
[TestClass]
public class UnitTest1 {
[TestMethod]
public void TestMethod1() {
}
}
}
The project is properly set as a test C# project. The tests in question also fail to show up in the Test List Editor's "Tests not in a list" section. (I translated most of the tests from VB.)
Why isn't Visual Studio recognizing the tests? How could I troubleshoot it?
Turns out that these tests were translated to C# from VB; when I translated them, I forgot to add explicit namespaces on them. Thus there were two test classes in the global namespace with the same name, which caused one to effectively replace the other. I just wish the warning given was a little less cryptic!

Type cant be resolve in UnitTest after migrating Project from vs2005 to vs2010 (MSTest)

We are actually analyzing what we have to do if we migrate our application from VS2005 up to VS2010.
What i have done:
I opened all solutions in VS2010 and let convert vs the projects.
At the moment the production assemblies dont get an upgrade of the .NET Framework,
it has to target the framework 2.
The framework version of the unit test assemblies (MSTest) is switched to the version 4 by VS2010 automatically,
thats ok so far.
The Problem:
Some unit tests are failing cause they can't access a config file through the ConfigurationMananger.OpenExeConfiguration(ConfigurationUserLevel.None) call.
The following exception is thrown:
System.Configuration.ConfigurationErrorsException: An error occurred
loading a configuration file: Type is not resolved for member X ...
System.Runtime.Serialization.SerializationException: Type is not
resolved for member X
The member X is derived from GenericIdentity and is marked as [Serializable].
All needed files (configuration, assemblies) are up to date and are correctly deployed in the output folder.
I tried to switch the framework version of the production assemblies to version 4, but it didn't help.
I found this ressources, but they dont helped me.
post from stack
Anybody has an idea why i get the described behavior?
If i change my Identity from
public MyIdentity : GenericIdentity
{ }
to
public MyIdentity : MarshalByRefObject, IIdentity
{ }
all of my tests are gettin green.

Makefiles in Microsoft Visual Studio 2010

I am getting some linking errors during the compilation of C project in Microsoft Visual Studio 2010.I am getting the following errors:
error LNK2019: unresolved external symbol _CreateRelation referenced in function _main
The CreateRelation is one of the functions in my project. Following are my questions:
I think it is some dependencies problem.How would I set those dependencies rule in the IDE?
could you please tell me, is it always possible to build a project and set the linking rule, how much it is larger, without using makefile?
[EDIT]
relation.h
void createRelation(LIST);
mainfile.c
#include relation.h
#include xyz.h
.
.
.
int main(){
LIST Relation1;
some codes //
createRelation(Relation1);
some code //
}
The function creatRelation() is defined in the realation.h.
EDIT 2
In the function containing main
There are a few ways to set the dependencies for the build process.
If the code you are referencing is in a sub-project you can simply tell VS the build-dependencies. You do that by right-clicking on the project and select project dependencies. Then you can check all projects that should be built before this project is being built.
Another nifty feature of VS2010 are Property Sheets. In older versions of VS you had to tell the compiler the include path and the lib-path for every project. Now you can create property sheets for every library you are using and then simply adding them to your project. This way you only have to create a property sheet once and can use it in many projects.
So if the code is in another project that is not a sub-project you have to set the lib-path and include-path via those property sheets. You can display the property sheets used by your project by clicking View->Additional Windows->Property Manager
If you are not referencing to any external projects. This problem is most likely caused by you not implementing a function you declared. So the compiler knows about the function-prototype and doesn't complain but the linker can't find an implementation of the symbol.
I hope that helps
-- edit --
Since you said that the implementation is in the same file as the main-function I would suspect that the signature of the declared and defined function do not match. Are you getting any warnings about implicit function declaration?
Is that a copy-paste error?
CreateRealtion(x); vs. CreateRelation(x);

Debugging Nunit Tests inside VS2010 Express

I have written a bunch of unit tests inside VS2010 Express and tests being tests they sometimes fail. Since the express editions of VS don't allow plugins to run I can't simply spin up TestDriven.Net or an equivalent and debug the tests. To try and work around this I've converted my test assembly into a console app and made the main method look like this:
class CrappyHackToDebugUnitTestInVSExpress
{
public static void Main()
{
AppDomain.CurrentDomain.ExecuteAssemblyByName(
#"C:\Program Files\NUnit 2.5.5\bin\net-2.0\nunit-console.exe",
new [] { Assembly.GetExecutingAssembly().Location, "/framework:4.0" });
}
}
In theory I should be able to run this up, set break points in my test. If it worked it would be an acceptable work around, but I keep getting the following:
FileLoadException
Could not load file or assembly 'C:\\Program Files\\NUnit 2.5.5\\bin\\net-2.0\\nunit-console.exe'
or one of its dependencies. The given assembly name or codebase was invalid.
(Exception from HRESULT: 0x80131047)
Now the file exists and when run manually nunit-console runs fine. What might be my problem?
Basically you need to convert your assembly to Windows Forms app, add reference to the nunit-gui-runner.dll assembly and change your Main method to look like this:
[STAThread]
static void Main()
{
NUnit.Gui.AppEntry.Main(new string[] { Assembly.GetExecutingAssembly().Location });
}
here is another example:
...
using NUnit.Gui;
namespace __libs
{
class Program
{
[STAThread]
static void Main(string[] args)
{
NUnit.Gui.AppEntry.Main(new string[] { #"C:\test\bin\Debug\test.exe" });
}
}
}
This will allow you to step into certain tests but is not very good for a red green cycle, so you will want to use this only when debugging and not in other circumstances.
I played with your concept and it appears the issue isn't directly from loading the file, but from dependencies.
I used the following modified code:
And the error was actually a failure to locate nunit.core.dll, which is in the /lib directory.
try
{
String NUnitPath = #"C:\Program Files\NUnit 2.5.7\bin\net-2.0\nunit-console.exe";
AssemblyName asmName = System.Reflection.AssemblyName.GetAssemblyName(NUnitPath);
AppDomain.CurrentDomain.ExecuteAssemblyByName(asmName, new[] { Assembly.GetExecutingAssembly().Location, "/framework:4.0" });
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
Trace.WriteLine(ex.StackTrace);
}
(I like getting System.Reflection.AssemblyName because you can inspect and see that everything's in order verses the raw file path.)
A quick bulk copy (xcopy nunit.*.dll) into my test projects' debug directory and it ran just fine. (It should be trivial to discover the minimal dependencies required)
Tested in VC# 2010 Express with NUnit 2.5.7 (breakpoints work, but I didn't really play with any other options.) Although I'm sure you could make a passable build option from it.
Cheers!
PS - First post here so I'm a bit untested as to getting the 'code' blocks formatted. Sorry in advance..
I had a similar problem trying to debug unit tests in VS C# express. Had a hard time getting it to work properly but then I found out about this project template. Works perfectly in C# Express!
http://visualstudiogallery.msdn.microsoft.com/b8a7a8fa-9f5a-4b9b-8e8b-8839a4364f26?SRC=VSIDE
C# Project Template
Integrated tests with Visual Studio, including Visual C# Express
version
Self contained NUnit console runner. Allow to write test fixtures and
test, running from Visual Studio simply by pressing F5 (support test
debugging), or Ctrl-F5 free run with results in console window. In
case of test failure indicate by beep sound.
Contains essential NUnit modules to start test project. No external
dependencies. Simply create new project, using NUnit Test Application
template.

Resources