Robolectric different threads in Android Studio vs. Gradle tests - unit-testing

why are thread names different when I start a Robolectric test from Android Studio vs. the Gradle build?
e.g. consider this simple test:
#RunWith(RobolectricTestRunner.class)
public class RobolectricRxThreadTest {
#Test
public void testMainThreadName() {
assertEquals("main", Thread.currentThread().getName());
}
}
when I start this test
directly in Android Studio (by pressing CTRL+SHIFT+F10) the test is okay
but when I start the gradle tests (e.g. from the Gradle projects view: :app - Tasks - verification - test, or from the Terminal view: ./gradlew test) it fails.
The thread name is Thread worker and not main as expected.

I just reproduced the issue, but I wonder why do you need this test?
If you really want to check main thread then the correct way would be:
assertThat(Thread.currentThread()).isEqualTo(Looper.getMainLooper().getThread());
And this test pass from AS and console both runs.

Related

Robolectric unit tests using legacy instead of binary resources only when running multi-module unit test run config

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.

Running (x)Unit Tests on TFS Build Pipeline

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)

Build and Execute Xamarin.iOS Unit Test from Command Line

I have been trying to run the iOS unit test from command line. I am using Xamarin Studio on Mac for now, I Created a iOS Unit test project as follows
Add New Project --> iOS --> Tests --> Unit Test App
And added a simple Unit test class and its code snippets as shown below:
using System;
using NUnit.Framework;
namespace iOSUnitTest
{
[TestFixture]
public class iOSTestSample
{
public iOSTestSample()
{
}
[Test]
public void MySampleTest() {
Assert.True(true);
}
[Test]
public void MyFailerTest()
{
Assert.False(true);
}
}
}
From Xamarin Studio, I am able to run the application which deploys a app into simulator and execute the test cases. I am trying to automate it through a script.
Till now, I am able to get the Unit Test project build and install the app into the running simulator.
I am not sure how to automate the unit test execution once the app is installed.
This blog post (and the ones linked( might be a bit out-dated but it shows you how to automate unit testing (both simulator and device builds) with the tools that ships with Xamarin.iOS.

NUnit TestCaseAttribute causing AmbiguousMatchException with NUnit VS Adapter

I wrote a bunch of unit tests that utilized the TestCaseAttribute. These tests run great on my local machine when I use the ReSharper unit test runner. Unfortunately, when I run the tests through Visual Studio with the NUnit VS Adapter 2.0.0.0 I get the following output:
------ Run test started ------
NUnit VS Adapter 2.0.0.0 executing tests is started
Loading tests from D:\Projects\Ever\WebApp\Ever.UnitTests\bin\Debug\Ever.UnitTests.dll
Exception System.Reflection.AmbiguousMatchException,
Exception thrown executing tests in
D:\Projects\Ever\WebApp\Ever.UnitTests\bin\Debug\Ever.UnitTests.dll
NUnit VS Adapter 2.0.0.0 executing tests is finished
========== Run test finished: 0 run (0:00:00.8290488) ==========
We use the Visual Studio Online hosted build server for our build, and that relies on the test adapter to run our NUnit unit tests. This means I need to figure out a way to make this work with the attribute (much preferred) or I have to work around this limitation.
Do I have to abandon the use of the TestCaseAttribute because MSTest doesn't support parameterized tests1,2?
After further debuging and testing I've concluded that the TestCaseAttribute isn't the cause of the issue. I'm answering my own question instead of deleting1 in case anyone else falls into the same trap that I did.
The TestCaseAttribute works properly as you can see with the following tests. These tests run perfectly well via the VS Test Adapter and the ReSharper test runner.
[TestFixture]
public class SimpleReproAttempt
{
[Test]
[TestCase(true, false)]
[TestCase(false, true)]
public void DoesNotReproduceIssue(bool a, bool b)
{
Assert.IsTrue(a || b);
}
[Test]
[TestCase("", false, true)]
[TestCase(null, true, false)]
public void DoesNotReproduceIssue(string a, bool b, bool c)
{
Assert.IsTrue(b || c);
Assert.IsNullOrEmpty(a);
}
}
The issue seems to be present only in tests that have an overloaded method with at least one of the overloads using async/await.
1: Editing my question based on this information would turn this into a chameleon question, and a chameleon via self answer is discouraged so I dismissed that option as well.

How to run tests in Nemerle project

How do I run tests which will test my nemerle code. So for example, I've a Calculator class and a CalculatorTests class in a nemerle project. I have already added a reference to nunit using package manager ("install-package nunit"). Now NUnit is available in nemerle project.
After writing following code
[TestFixture]
class CalculatorTests
{
[Test]
MyTest() : void
{
def result = Calculator().Add( 1 );
Assert.AreEqual( 2, result );
}
}
I tried to use TestDriven.net visual studio add-in to run the test but couldn't able to. Can someone tell me how to run tests in nemerle or do i have to write code to run all tests when executing a console app?
Maybe it caused by NUnit runs under .Net 2.0 runtime. Try to set a runtime version in the NUnit command line.