I have a class in my Xamarin PCL which makes a call to System.Reflection.GetRuntimeProperties. For an example, let's say my PCL class has this method:
public string ExampleMethod(string arg) {
if(arg == null) return null;
IEnumerable<PropertyInfo> infos = this.GetType().GetRuntimeProperties();
return infos[0].Name;
}
I then have a Xamarin.UITest project which references the PCL project and tests this class. I have two test cases in my TestFixture so far, which for our example would be the following:
[Test]
public void TestExampleMethod_ArgNull_Null(){
Assert.That (exampleInstance.ExampleMethod(null), Is.Null);
}
[Test]
public void TestExampleMethod_ArgNotNull_NotNull(){
Assert.That (exampleInstance.ExampleMethod("testValue"), Is.NotNull);
}
When I run the Xamarin.UITest project, it compiles, runs the tests, and completes fine on both Android and iOS platforms. The TestExampleMethod_ArgNull_Null test passes since it returns early. However, the TestExampleMethod_ArgNotNull_NotNull test fails with:
System.MissingMethodException : Method 'RuntimeReflectionExtensions.GetRuntimeProperties' not found.
So it appears that even though everything is compiling just fine, and I am able to run other test cases fine, the Xamarin.UITest project is not able to use anything in System.Reflection. Does anyone know how I go about debugging this?
On my end, using the following failed to build:
IEnumerable<PropertyInfo> infos = this.GetType().GetRuntimeProperties();
return infos[0].Name;
due to not being able to do bracket indexes on and IEnumerable. I changed to this:
List<PropertyInfo> infos = this.GetType().GetRuntimeProperties().ToList();
return infos[0].Name;
And the project built and the tests ran successfully.
The class with the method using Reflection was in a PCL which was referenced from a UI Test project.
So basically I am not able to reproduce the error you got.
I posted this to Xamarin Support as well (thanks #jgoldberger) and we were able to figure out that it was due to a project setup issue. This is a project which uses Couchbase Lite which requires a specific version of Json.Net (6.0.4 as of this post). I must have accidentally updated the packages on some of the projects since the same version of Json.Net was not being used across all the projects (PCL, Android, iOS, and UITest). I ended up re-creating the project from scratch and that took care of it.
Related
I have a multi-module android project. I have a bunch of unit tests in each module and I have always been able to run them all at once using a run configuration like this one:
Many of my tests use a base class that runs with RobolectricTestRunner. This base class looks like this:
#RunWith(RobolectricTestRunner::class)
#Config(application = AndroidTest.ApplicationStub::class,
manifest = Config.NONE,
sdk = [21])
abstract class AndroidTest {
#Suppress("LeakingThis")
#Rule #JvmField val injectMocks = InjectMocksRule.create(this#AndroidTest)
fun application(): Application = ApplicationProvider.getApplicationContext()
internal class ApplicationStub : Application()
}
**When running these tests using the above config, I get the error **
[Robolectric] NOTICE: legacy resources mode is deprecated; see http://robolectric.org/migrating/#migrating-to-40
This makes many of my tests fail with ResourceNotFoundException
However, when I run tests only in a specific module, everything passes. This is because Robolectric now uses Binary resources:
[Robolectric] sdk=21; resources=BINARY
I have followed the migration instructions in build.gradle files for each module, having added the following in each android block:
testOptions {
unitTests {
includeAndroidResources = true
returnDefaultValues = true
}
}
One clue I have found but have been unable to fix is this when I run the ALL UNIT TEST task:
WARNING: No manifest file found at build/intermediates/merged_manifests/debug/../../library_manifest/debug/AndroidManifest.xml.
Falling back to the Android OS resources only.
No such manifest file: build/intermediates/merged_manifests/debug/../../library_manifest/debug/AndroidManifest.xml
To remove this warning, annotate your test class with #Config(manifest=Config.NONE).
I have tried, as you have seen, to add the manifest=Config.NONE, which had no effect (and is now deprecated anyway).
Edit: Also tried android.enableUnitTestBinaryResources = true in settings.gradle, but this prevents the app from building due to it being a deprecated flag in the current gradle tools.
Thanks for any help provided!
So with the default unit test run platform being changed to Gradle in Android Studio, I managed to figure out a way to run unit tests in multiple modules all at once, circumventing the Robolectric bug.
First, go into run configurations and create a new Gradle Config.
Then, as the gradle project, select the top level project.
For arguments, use --tests "*"
And now for the gradle tasks, this is a little bit more error-prone. Here is an example of how I have it setup for my project:
:androidrma:cleanTestGoogleDebugUnitTest :androidrma:testGoogleDebugUnitTest
:calendar:cleanTestDebugUnitTest :calendar:testDebugUnitTest
:gamification:cleanTest :gamification:test
:player:cleanTest :player:test
:playlists:cleanTest :playlists:test
:sleepjournal:cleanTest :sleepjournal:test
:sound-content-filters:cleanTest :sound-content-filters:test
Please note that I inserted new lines between each module for more clarity here, in the tasks, just separate each entry with a space.
For your app module, in my case named androidrma, you must use your build variants name in the cleanTestUnitTest and testUnitTest , in my case being GoogleDebug.
If we look at the calendar module, it is an android module, , so it still operates with the same logic as the appModule.
However, if you look at player, playlists, sleepjournal, etc. those are pure kotlin modules. The tasks thus differ in their syntax.
Once you have entered all this information and everything is functioning, I recommend checking "store as project file" checkbox at the top right of the run config setup screen.
This works in Android Studio 4.2 as well as Arctic Fox, haven't tested on other versions.
I am merely a beginner and still trying to learn about TFS and its continuous integration workflow. Having that said, this could as well be a stupid question to ask as I might be missing on a simple detail, though any help or advice would be highly appreciated.
So, I have a fairly simple Unit Test example written using .NET Core 2.0, which I would like to run as a test task on our TFS Server's CI Build pipeline. It pretty much looks something like this:
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MyUnitTest
{
[TestClass]
public class MyUnitTest
{
[TestMethod]
public void PassingTest()
{
Assert.AreEqual(4, Add(2, 2));
}
[TestMethod]
public void FailingTest()
{
Assert.AreEqual(5, Add(2, 2));
}
int Add(int x, int y)
{
return x + y;
}
}
}
When I try to run these tests in Visual Studio, it builds perfectly and the tests succeed and fail accordingly. So I commit my project and push it to our TFS git repository. Now, I similarly would like to integrate these tests in our build pipeline.
The Build Definition used in our CI builds looks like this. I have added Visual Studio Test - testAssemblies task in the build pipeline and configured the search pattern to find the assembly named MyUnitTest.dll and such. When I queue the build, I get the following warning in VSTest's log.
Warning: No test is available in C:\BuildAgent\_work\9\s\MyUnitTest\MyUnitTest\bin\release\netcoreapp1.1\MyUnitTest.dll. Make sure that installed test discoverers & executors, platform & framework version settings are appropriate and try again.
So, it seems to me that VSTest somehow cannot find tests to run in the target assembly. I am pretty positive that I might have misconfigured something, or forgotten to set some particular parameter appropriately. I will be more than grateful for any suggestion that would possibly pinpoint what I might be doing wrong.
Searching for solutions online, I have come across this question, which seems to have a similar problem.
First make sure your build agent environment is as same as your local develop machine. Such as Visual Studio version, MsTestAdapter ,xunit runner version and so on.
You could double confirm this by manually run the test directly on the build agent machine not through TFS.
Then use the below tasks in your build pipeline:
Add a dotnet restore task.
Then a dotnet build task.
Add a dotnet test task with the arguments --no-build --logger "trx;LogFileName=tests-log.trx
Add a Publish test results task with the following settings
More details please refer this tutorial blog: Running dotnet core xUnit tests on Visual Studio Team Services (VSTS)
I'm currently in the process of increasing code coverage on our software products and have ran into an issue; all of my unit tests (when compiled using 'Any CPU') are failing due to throwing a 'BadImageFormatException'.
This exception can be circumvented by building the solution using 'x86' instead of 'Any CPU', however the requirements are such that we need to be able to run them using Any CPU/x64 bit.
All unit tests involving Moq follow pretty much the same format:
[TestMethod]
public void GetProduct_ValidId_ProductReturned()
{
//Setting up the object
Product prod = new Product();
prod.ID = 7;
prod.Name = "Test";
//Create the mocks
var mockProductRepo = new Mock<IRepository<Product>>();
var testDb = new Mock<IUnitOfWork>();
//Setup what the repo needs to return, in this case it's a Product
mockProductRepo.Setup(m => m.getByID(7)).Returns(prod);
//Setup what the database needs to return, in this case it's our repo which we've already setup
testDb.SetupGet(m => m.ProductRepo).Returns(mockProductRepo.Object);
//Run the test
Product returnedProd = ProductHelper.getProduct(testDb.Object, 7);
Assert.IsNotNull(returnedProd);
}
I can confirm that this test is successful when it is built using x86. Does anyone have any ideas on how I can get Moq to play nice when built using 'Any CPU'?
As an aside I can also confirm that all my projects in the solution are build using the same value ('Any CPU'). I'm using Moq v4.0.
EDIT: Here is the full exception: Test method [ProductName and the method called] threw exception:
System.BadImageFormatException: Could not load file or assembly '[Product name], Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
Ok so after some digging I finally found out what the issue was. Even if you select 'Build' and then 'Configuration Manager' from the toolbar and see that Platform is set to 'Any CPU' (as was my case), what I hadn't done was check the Target Platform in the project.
To check the Target Platform you need to do the following:
Right-click your project and select 'Properties'
Select the 'Build' tab on the left
Ensure that the Target Platform of your test project matches that of the project you are testing
In my case my test was targeting 'Any CPU' but my live project was targeting 'x64'. This is what was causing the issue.
This can be caused by missing project or other assembly references. Try making sure you have project references for all the projects in your solution.
This post has a further example.
I am busy converting by existing firebreath plugin here to use gpgme instead of making calls via the OS and the gpg binary.
I have managed to get the code to compile in windows using VS 2010 on a x32 system but after loading the plugin into chrome I can not access the npapi code at all. Even simple version calls fails.
When loading the plugin I get no visible errors but when using sawbuck log viewer for chrome I get the erorr messages below.
.\renderer\webplugin_delegate_proxy.cc 347 PluginMsg_Init returned false
..\plugins\npapi\webplugin_impl.cc 271 Couldn't initialize plug-in
I have tried to use my code with both firebreath 1.4 and 1.6 and neither versions work. After some simple debugging it seems that using any code provided by gpgme (whether its called or not) causes the plugin to break.
I came to this conclusion by doing the following.
Created a new project with firebreath (versions 1.4 and 1.6)
Add the gpgme.h headers to gmailGPGAPI.cpp and changed nothing else aside from adding the required reference paths to the project.
Build the project to create the dll (this generates the dll fine).
Replace the existing ddl in my project with the dll in step 2 and test it with the following piece of code
plugin = document.createElement('object'); plugin.id = 'plugin';
plugin.type = 'application/x-gmailtest';
document.body.appendChild(plugin);
console.log("my plugin returned: "+ plugin.valid);
console.log("my plugin returned: " + plugin.version);
This returns valid = true and the version returns what ever i set it to.
I then modified gmailGPGAPI.cpp to now return the gpg version by calling gpgme_check_version(NULL) in the version method. I used that method because its probably the simplest returning function that I could test with.
Build the plugin and copy dll to chrome extension as in step 3-4. The plugin builds fine again as expected.
Load the plugin and try to execute the code in step 4 at which point it now just returns undefined for any property or method i try to access on the plugin. No errors are printed to the console or anywhere else in chrome except for the error logged to sawbuck.
I have got no idea where to look or what to try since I cant seem to get an actionable error to work against. I have also reduced by test code to the point where its just a new project with a one line change to make it easier to find the problem.
I should note the code in the repo builds fine in linux/OSX and loads into chrome correctly so I know at some level my code does work.
Two possible paths:
You may have a DLL dependency that isn't available which keeps the plugin from loading; if you run regsvr32 on it in the state where it doesn't work on chrome, does it work?
Your plugin may be loading and then crashing. Start chrome with --plugin-startup-dialog and then when it pops up a dialog warning you that a plugin is about to be loaded attach to that process and see if the process crashes. At this point you can also set breakpoints to try to figure out how far it gets.
Double check your metadata in PluginConfig.cmake as well; sometimes unusual characters in some fields can cause issues like this.
I am developing a Wp7-App and I want to start Unit Testing. I used the Template from Visual Studio 2010 to create a Windows Phone 7.1 UnitTest-Project and I added the required Assemblies via Nu-Manager.
I can't start the project in emulator or on a real device. I get a blank loading-screen and this error message:
A first chance exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll
Is this a known error? Is there a workaround?
Thanks!
The standard VS Unit test template won't work for WP7. You should look at these links:
http://channel9.msdn.com/Events/MIX/MIX10/CL59
and
http://www.jeff.wilcox.name/2011/06/updated-ut-mango-bits/
I have a Silverlight 4 Test Project using the Jeff Wilcox assemblies and it works fine for my WP7 tests. I use [TestClass] and [TestMethod] attributes and these namespaces inside my tests:
using Microsoft.Silverlight.Testing; using
Microsoft.VisualStudio.TestTools.UnitTesting;
Within the App.xaml.cs file there is minimal code and the following starts it all off:
private void Application_Startup(object sender, StartupEventArgs e)
{
RootVisual = UnitTestSystem.CreateTestPage();
}