Microsoft Fake Assemblies - unit-testing

I'm trying to generate a ShimDirectory (from System.IO.Directory). In the test project references I right click on System and select Add Fakes Assembly. This creates mscorlib.fakes and System.fakes in the Fakes folder.
However, the namespace System.IO.Fakes contains only six class (all Stub...) and no ShimDirectory, or any other shims I would expect.
Any ideas?
(Using VS2017)

Okay - turns out the trick is to restart Visual Studio, delete the fakes and regenerate them and it works!

Related

How do I Unit Test and mock a C project in Visual Studio

I have a Project written in Visual Studio 2019 in C. I wish to Unit Test individual parts of my code and also be able to mock some functions off. What is the best way to go about this?
I have tried creating a new Unit Test Project within the same Solution as my production code, in the Native Unit Test framework for C++ in Visual Studio. The problem is I have to include my library files in every Unit Test project that I make which becomes tedious. I have tried the Fake Function Framework for C, but was unable to make it work.
I am open to new ideas, however it has to be in Visual Studio since my production code is placed in a project in Visual Studio.
Edit: A picture of my Solution Structure is shown in the picture here
I have to include the library files from my productionCode project inside my unit test project before it will build.
Not sure if this is an answer to my own question, but what I ended up doing was splitting my large project into smaller components and then write unit tests for each component instead of including library files from the large project. This way I could fake off the dependencies using the Fake Function Framework.
Thanks for the help

How to select/edit proper folder for unit test creation in Android Studio?

In Android studio, I used to be able to generate a unit test for a class really easily by using the hotkey: CMD + SHIFT + T. It would always find the correct folder for placing it (obviously with other unit tests)
but for some reason, after switching to gradle, shifting some tests folders around, Android studio seems really confused and the hotkey tries to create a unit test under the generated folders. and there's no way to select what parent folder i want (from system)
is there a way to modify this somewhere?
Unless you made changes to where Android Studio expects your sourcesets (done in the build.gradle file), you cannot move where your tests are. By default studio expects your tests to be in src/androidTest/java/package (make sure you are in at least version .46), once you create this directory you will see the java folder appear as green which is a recognized test folder by studio. In IntelliJ you were able to change the test sources within the project structure window, but in Android Studio this screen seems to not exist. Now when you use cmd+shift+T it will work correctly

VS2012 & TFS2012 Unit Test major issues

We are using VS2012 and TFS2012 and write unit tests for our code. We want to report code coverage, and also using .config files in our unit tests for test appsettings, and also some other settings for logging, MS Enterprise library settings etc. etc.
App.config not working in new test framework
New test framework of MS should be great, but to me it is not so great at all.
How I'm i suppose to set some basic configuration in config files, when the new framework does not use config files anymore?
We had a problem with mixed mode dlls, and found a fix: adding
<startup useLegacyV2RuntimeActivationPolicy="true">
to the app.config. But this did not work for our unit test project. Becuase config files are not there anymore. Searching the internet came up with a solution
'Problems with .Net 2.0 Mixed Mode Assemblies inside Visual Studio .Net 4.5 Test Projects'
This means editing a file of Visual Studio 11 itself in the program files directory, not a great solution i think....
And how about some basic appsetting? How am I supposed to set this?
Do not use the .testSettings file
Using the old .testsettings file is also not recommended by MS, becuase then the old test framework is used. And if I use the .testsettings file, i cannot setup Code Coverage on my tfs2012 build service.
Another issue is that we have code that need a dll (system.data.sqlite.dll), but only at runtime the unit test code needs this dll. So a reference is not needed. We fixed this by using the Deployment tab on the testsettings file. But in the new framework, you should not use the testsettings file. You have the [deploymentitem] attribute if you need files. But the deploymentitem attribute can only be used on a [testmethod] not on a [testinitialize] or [assemblyinitialize] method. But our code needs the dll in the [testinitialize] method. So there is no way to get the dll in place.
Just copy it with File.Copy in the [assemblyinitialize] (or testinitialize) method does not work.
Adding the dll as file to the project, and set the 'copy to output directory' to 'Copy Always' as mentioned in 'Configuring Unit Tests by using a .runsettings File' also does not work at all.
The (really not great) solution for this is to add the dll as a reference, then instantiate a class and do nothting with it. This way the dll is needed otherwise it is not building, and thus the dll will deploy itself to the right directories.
how to solve my problem(s)???
- I want to use config files in my unit test.
- I want to deploy some files that are neede in the 'assemblyinitialise' and/or 'classinitialize' methods.
- I want Code Coverage on my TFS2012 nightly build enabled.
a) App.config not working in new test framework
This should still work. What I think is missing in this case is that this .config file is not being copied with your test dll. Could you please set this as a deployment item and try again?
b) Do not use the .testSettings file
.testsettings and code coverage.
Setting up code coverage with the .testsettings file IS still supported in VS 2012 build. You simply need to select the mstest 2010 test runner and specify your .testsettings file in your build definition
If you dont have anything except code coverage settings in the .testsettings file then you can easily migrate to the 2012 test runner and select "enable code coverage" in the drop down items
copying a file required by test initalize
You could do this via the .testsettings file or you can have a post-build file copy task. It is pretty straight forward to do so and has no impact on anything else. Using the "copy to output directory = copy always" does work. Please try it with a sample solution and see if you can narrow down on why this does not work on your setup.

VS2012 - add test missing?

In VS 2010, I could right click a method and have MSTest create a unit test for me. Where is this feature in VS2012 pro?
The command is still there. go to Tools->Options->Environment->Keyboard. Search the shortcuts for createunittests. The appropriate command comes up in the dialog box. simply assign it a keyboard shortcut and its ready to go. Works exactly like the VS2010 once you use the shortcut you created.
Just ran into this myself just now. Here is the explanation:
http://connect.microsoft.com/VisualStudio/feedback/details/748493/missing-funtionality-of-creating-a-new-unit-test-from-editor
"The 'New Test' menu does not have any extension points where 3rd party adapters can plug-in. Therefore we decided to remove it. We moved all the New Test Project types into the File -> New -> Project dialog."
So it seems it was removed because it was made for MSTest only, and now they support several frameworks out of the box. But I still miss it as well... Should be able to default to MSTest och NUnit...
Should select Add not New item in FILE menu to add a Test project into current solution.
below steps are copied from Walkthrough: Creating and running unit tests for managed code
To create a unit test project:
On the File menu, choose Add, and then choose New Project ....
In the New Project dialog box, expand Installed, expand Visual C#,
and then choose Test.
From the list of templates, select Unit Test Project.
Hope it helpful.

Visual Studio C++: Unit test exe project with google test?

Using Visual Studio 2010 C++. I'm experimenting with unit testing and decided to try Google Test (gtest). I have an existing project which compiles to an MFC executable (I'm also interested in how to test a project that compiles to a DLL). My understanding of the convention for unit testing is that you should create a new separate project for your tests. So I created a new project in the same solution for my unit tests. But how do I link the projects? Can I test arbitrary functions/methods of my exe project from my test project?
What is the conventional way to do this?
I think the best way to organize unitary test is:
Don't change your main project. The structure should be independent of your test actions. In my opinion, changing your project to a big static lib AND an executable is really not elegant.
Instead, add a post build action to aggregate all obj files into a static lib file that will be used ONLY by your test project.
Create a simple test project, linking to your test framework AND the static library you have previously generated.
Enjoy.
The main advantages is you don't touch the project you want to test and you don't include all source code to your test project.
To see how you can do that for visual studio, you can see this post: Linking to multiple .obj for unit testing a console application
Either put the functionality you want to test into a static library which is linked into both your test project and your MFC project, or put your files in both projects. The first is more complicated, but the second will cause you to compile everything twice....
I have prepared a github repo including Visual Studio 2015 solution in parralel of Billy's answer. You can use it directly without any additional requirement or dependency.
https://github.com/fuatcoskun/GoogleTestVS2015
I hope it helps...