Question to thee group

37 views
Skip to first unread message

Baubak

unread,
Sep 20, 2021, 6:01:05 AM9/20/21
to testng-users
Hi everyone,

Just wanted your opinion on how you manage tests in my case.

I recently logged a bug in TestNG which will be corrected in the next release (Thanks TestNG). I have created my unit test to prepare for the change.


Obviously it will start working when I implement the change, until hen my est will correctly fail.

Now my question is how do you manage such tests that are planned to be fixed in a forseeable future? Do you comment the test until the release is done?

JUnit had a system called assume that would thoretically work, but I do not think it is that feasible in general nor in this case. So I am interested in your opinions.

Best regards,

Baubak

⇜Krishnan Mahadevan⇝

unread,
Sep 20, 2021, 9:49:36 AM9/20/21
to testng-users
While others chime in with their observations, here's something that you can do.

  1. Define a custom annotation that can accept a condition which can be used to enable/disable a test method.
  2. Define a custom annotation transformer (which you wire in using service provider interface) that can introspect this custom annotation to retrieve the condition, instantiate it via reflection, evaluate the condition and then use that outcome to enable/disable a test.
Here's a full fledged example that explains what I am hinting at.

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;

import java.io.File;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import org.testng.TestNG;

@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({METHOD, TYPE})
public @interface Condition {

    Class<? extends TestCondition> test();

    interface TestCondition {
        boolean test();
    }

    class TestNGFutureVersion implements TestCondition {

        @Override
        public boolean test() {
            String file = TestNG.class.getProtectionDomain().getCodeSource().getLocation().getFile();
            return new File(file).getAbsolutePath().contains("7.5.0");
        }
    }
}
import com.rationaleemotions.conditions.Condition.TestCondition;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;
import org.testng.internal.InstanceCreator;

public class ConditionBasedTransformer implements IAnnotationTransformer {

    @Override
    public void transform(ITestAnnotation annotation, Class testClass,
        Constructor testConstructor, Method testMethod) {
        Condition condition = testMethod.getAnnotation(Condition.class);
        if (condition != null) {
            Class<? extends TestCondition> predicate = condition.test();
            TestCondition instance = InstanceCreator.newInstance(predicate);
            annotation.setEnabled(instance.test());
        }
    }
}
import com.rationaleemotions.conditions.Condition.TestNGFutureVersion;
import org.testng.annotations.Test;

public class ExampleTestClass {

    @Test
    @Condition(test = TestNGFutureVersion.class)
    public void testMethod() {
        System.err.println("This can run only in TestNG 7.5.0");
    }
}

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/6c23de1f-e550-47f2-b2bb-4b4eac255f11n%40googlegroups.com.

Baubak

unread,
Sep 23, 2021, 5:08:39 PM9/23/21
to testng-users
Thanks a lot Krishnan.

This is a good solution. Any other ideas anyone?

Best regards,

Baubak
Reply all
Reply to author
Forward
0 new messages