AndroidJUnit4.class + org.junit.Assume.assumeTrue = AssumptionViolatedException

I managed to transfer the Android project to JUnit4, and, of course, the main reason I wanted to do this does not work. Would love to have any help if anyone got ideas here.

The problem I'm trying to solve is that I want to automatically skip certain tests if the assembly is not listed on an intermediate server. I configured it using BUILD_TYPE , which uses gradle to enter the base URL.

I set the assumeThat clause in my setup, which correctly identifies when the build fails, but instead of stopping and ignoring the rest of the test, it throws an exception and fails.

Here is my base class for my live API tests - I annotated the descent from this with @RunWith(AndroidJUnit4.class) , so theoretically this should always be done with the JUnit4 runner:

 package com.[my package].nonuitests.liveservertests; import android.support.test.runner.AndroidJUnit4; import com.[my package].nonuitests.BaseAndroidTest; import org.junit.Test; import org.junit.runner.RunWith; /** * Tests against the live API. All tests descending from this class will * be ignored if the BUILD_TYPE is not staging. */ @RunWith(AndroidJUnit4.class) public class BaseLiveServerTests extends BaseAndroidTest { private static final String STAGE = "staging"; /****************** * SETUP/TEARDOWN * ******************/ @Override public void setUp() throws Exception { super.setUp(); //TODO: YU NO WORK?! //This should cause the rest of the test to be skipped if it fails, //but is instead throwing an AssumptionViolatedException. assumeTrue(STAGE.equals(BuildConfig.BUILD_TYPE)); } } 

So my questions are:

  • Is there a better way to do this? Theoretically, this could be done with the help of fragrances, but I tried to do it before, and it made everything more complicated.
  • My research indicates that Google is not implementing in its runner what makes it bomb, but I get damn time to figure out what I can / should do to fix it. Any suggestions for things that I have to subclass in order to get this to work as expected?

Any other thoughts would be most appreciated. Thanks!

Edit (1/26/15 11:40 AM CST): Per Grzesuav's suggestion , I took a hit when implementing this as @Rule , but at the moment it still doesn't work. This seems like a promising way, but at the moment it is not working.

Edit 2 (1/26/15 12:15 pm CST): OK, now it works .

+5
source share
2 answers

https://github.com/junit-team/junit/wiki/Assumptions-with-assume

ad 2) Custom runners may handle accept statements differently. To fix this, you must write your own version of the Android runner and implement a way to deal with assumptions, since the native JUnit runner makes or makes a mistake for the runner for Android.

ad 1) Suggested by me: try using JUnit rules:

http://www.codeaffine.com/2013/11/18/a-junit-rule-to-conditionally-ignore-tests/ http://cwd.dhemery.com/2010/12/junit-rules/

+2
source

OK, finally, he worked with @Rules for the Grzesuav proposal , although with significant changes since the MethodRule deprecated. Here the essence , as it turned out, is that I will try to keep this update, as I will clarify it.

Some important notes:

  • You need to instantiate @Rule in the test class, otherwise you will never click any of your checks.
  • As of right now, this will not mark the test as ignored on Android, it will just pass it without checking anything.
+2
source

Source: https://habr.com/ru/post/1211906/


All Articles