Home | History | Annotate | Download | only in name
      1 package test.name;
      2 
      3 import org.testng.Assert;
      4 import org.testng.ITestResult;
      5 import org.testng.TestListenerAdapter;
      6 import org.testng.TestNG;
      7 import org.testng.annotations.Test;
      8 
      9 import java.util.ArrayList;
     10 import java.util.Arrays;
     11 import java.util.Collections;
     12 import java.util.List;
     13 
     14 import test.SimpleBaseTest;
     15 
     16 public class NameTest extends SimpleBaseTest {
     17 
     18   @Test
     19   public void itestTest() {
     20     TestNG tng = create(SimpleITestSample.class);
     21     TestListenerAdapter adapter = new TestListenerAdapter();
     22     tng.addListener(adapter);
     23 
     24     tng.run();
     25 
     26     Assert.assertTrue(adapter.getFailedTests().isEmpty());
     27     Assert.assertTrue(adapter.getSkippedTests().isEmpty());
     28     Assert.assertEquals(adapter.getPassedTests().size(), 1);
     29     ITestResult result = adapter.getPassedTests().get(0);
     30     Assert.assertEquals(result.getMethod().getMethodName(), "test");
     31     Assert.assertEquals(result.getName(), "NAME");
     32     Assert.assertEquals(result.getTestName(), "NAME");
     33   }
     34 
     35   @Test
     36   public void itestTestWithXml() {
     37     TestNG tng = createTests("suite", SimpleITestSample.class);
     38     TestListenerAdapter adapter = new TestListenerAdapter();
     39     tng.addListener(adapter);
     40 
     41     tng.run();
     42 
     43     Assert.assertTrue(adapter.getFailedTests().isEmpty());
     44     Assert.assertTrue(adapter.getSkippedTests().isEmpty());
     45     Assert.assertEquals(adapter.getPassedTests().size(), 1);
     46     ITestResult result = adapter.getPassedTests().get(0);
     47     Assert.assertEquals(result.getMethod().getMethodName(), "test");
     48     Assert.assertEquals(result.getName(), "NAME");
     49     Assert.assertEquals(result.getTestName(), "NAME");
     50   }
     51 
     52   @Test
     53   public void testNameTest() {
     54     TestNG tng = create(NameSample.class);
     55     TestListenerAdapter adapter = new TestListenerAdapter();
     56     tng.addListener(adapter);
     57 
     58     tng.run();
     59 
     60     Assert.assertTrue(adapter.getFailedTests().isEmpty());
     61     Assert.assertTrue(adapter.getSkippedTests().isEmpty());
     62     Assert.assertEquals(adapter.getPassedTests().size(), 1);
     63     ITestResult result = adapter.getPassedTests().get(0);
     64     Assert.assertEquals(result.getMethod().getMethodName(), "test");
     65     Assert.assertEquals(result.getName(), "NAME");
     66     Assert.assertEquals(result.getTestName(), "NAME");
     67   }
     68 
     69   @Test
     70   public void testNameTestWithXml() {
     71     TestNG tng = createTests("suite", NameSample.class);
     72     TestListenerAdapter adapter = new TestListenerAdapter();
     73     tng.addListener(adapter);
     74 
     75     tng.run();
     76 
     77     Assert.assertTrue(adapter.getFailedTests().isEmpty());
     78     Assert.assertTrue(adapter.getSkippedTests().isEmpty());
     79     Assert.assertEquals(adapter.getPassedTests().size(), 1);
     80     ITestResult result = adapter.getPassedTests().get(0);
     81     Assert.assertEquals(result.getMethod().getMethodName(), "test");
     82     Assert.assertEquals(result.getName(), "NAME");
     83     Assert.assertEquals(result.getTestName(), "NAME");
     84   }
     85 
     86   @Test
     87   public void noNameTest() {
     88     TestNG tng = create(NoNameSample.class);
     89     TestListenerAdapter adapter = new TestListenerAdapter();
     90     tng.addListener(adapter);
     91 
     92     tng.run();
     93 
     94     Assert.assertTrue(adapter.getFailedTests().isEmpty());
     95     Assert.assertTrue(adapter.getSkippedTests().isEmpty());
     96     Assert.assertEquals(adapter.getPassedTests().size(), 1);
     97     ITestResult result = adapter.getPassedTests().get(0);
     98     Assert.assertEquals(result.getMethod().getMethodName(), "test");
     99     Assert.assertEquals(result.getName(), "test");
    100     Assert.assertEquals(result.getTestName(), null);
    101   }
    102 
    103   @Test
    104   public void noNameTestWithXml() {
    105     TestNG tng = createTests("suite", NoNameSample.class);
    106     TestListenerAdapter adapter = new TestListenerAdapter();
    107     tng.addListener(adapter);
    108 
    109     tng.run();
    110 
    111     Assert.assertTrue(adapter.getFailedTests().isEmpty());
    112     Assert.assertTrue(adapter.getSkippedTests().isEmpty());
    113     Assert.assertEquals(adapter.getPassedTests().size(), 1);
    114     ITestResult result = adapter.getPassedTests().get(0);
    115     Assert.assertEquals(result.getMethod().getMethodName(), "test");
    116     Assert.assertEquals(result.getName(), "test");
    117     Assert.assertEquals(result.getTestName(), null);
    118   }
    119 
    120   @Test
    121   public void complexITestTest() {
    122     TestNG tng = create(ITestSample.class);
    123     TestListenerAdapter adapter = new TestListenerAdapter();
    124     tng.addListener(adapter);
    125 
    126     tng.run();
    127 
    128     Assert.assertTrue(adapter.getFailedTests().isEmpty());
    129     Assert.assertTrue(adapter.getSkippedTests().isEmpty());
    130     Assert.assertEquals(adapter.getPassedTests().size(), 5);
    131     List<String> testNames = new ArrayList<>(Arrays.asList("test1", "test2", "test3", "test4", "test5"));
    132     for (ITestResult testResult : adapter.getPassedTests()) {
    133       Assert.assertTrue(testNames.remove(testResult.getName()),
    134                         "Duplicate test names " + getNames(adapter.getPassedTests()));
    135     }
    136     Assert.assertEquals(testNames, Collections.emptyList());
    137   }
    138 
    139   private static List<String> getNames(List<ITestResult> results) {
    140     List<String> names = new ArrayList<>(results.size());
    141     for (ITestResult result : results) {
    142       names.add(result.getName());
    143     }
    144     return names;
    145   }
    146 }
    147