How to use TestFinder class of net.serenitybdd.junit.finder package

Best Serenity JUnit code snippet using net.serenitybdd.junit.finder.TestFinder

Source:TestFinder.java Github

copy

Full Screen

...7import org.junit.runner.RunWith;8import java.lang.reflect.Method;9import java.util.*;10/**11 * The TestFinder class lets you find the Thucydides tests or test methods underneath a given package.12 * <p>You instantiate a TestFinder by providing the top-level package where the tests live.</p>13 * <p>You can then find the list of Thucydides test classes using getNormalTestClasses(), getDataDrivenTestClasses(),14 * and getAllTestClasses() (which returns both normal and data-driven tests).</p>15 * <p>You may also need to retrieve the list of test methods for a particular category of class. You can do this using the16 * getTestMethodsFrom() method, e.g.17 * <pre>new TestFinder("my.package").getTestMethodsFrom().normalTestClasses()</pre>18 */19public abstract class TestFinder {20 protected final String rootPackage;21 /**22 * Create a new test finder instance that will look for tests in the packages underneath the given root package.23 * @param rootPackage The root package used as the starting point when looking for test classes.24 */25 protected TestFinder(final String rootPackage) {26 this.rootPackage = rootPackage;27 }28 public static TestFinderBuilderFactory thatFinds() {29 return new TestFinderBuilderFactory();30 }31 public abstract List<Class<?>> getClasses();32 public abstract int countTestMethods();33 public TestMethodFinder findTestMethods() {34 return new TestMethodFinder(this);35 }36 protected List<Class<?>> getAllTestClasses() {37 return ClassFinder.loadClasses().annotatedWith(RunWith.class).fromPackage(rootPackage);38 }39 protected Set<Class<?>> getNormalTestClasses() {40 Set<Class<?>> normalTestClasses = new HashSet();41 for(Class<?> testClass : getAllTestClasses()) {42 if (normalThucydidesTest(testClass)) {43 normalTestClasses.add(testClass);...

Full Screen

Full Screen

Source:DataDrivenTestFinder.java Github

copy

Full Screen

...7import java.util.List;8/**9 * Returns all of the Thucydides classes under the specified package.10 */11public class DataDrivenTestFinder extends TestFinder {12 public DataDrivenTestFinder(final String rootPackage) {13 super(rootPackage);14 }15 @Override16 public List<Class<?>> getClasses() {17 return sorted(new ArrayList(getDataDrivenTestClasses()));18 }19 @Override20 public int countTestMethods() {21 int totalTestMethods = 0;22 for(Class testClass : getDataDrivenTestClasses()) {23 try {24 totalTestMethods += DataDrivenAnnotations.forClass(new TestClass(testClass)).countDataEntries();25 } catch (IOException e) {26 throw new IllegalArgumentException("Failed to read test data for " + testClass);...

Full Screen

Full Screen

Source:TestFinderBuilder.java Github

copy

Full Screen

1package net.serenitybdd.junit.finder;2public class TestFinderBuilder {3 public static TestFinderBuilder on(FinderType finderType) {4 return new TestFinderBuilder(finderType);5 }6 private final FinderType finderType;7 protected TestFinderBuilder(FinderType finderType) {8 this.finderType = finderType;9 }10 public TestFinder inPackage(final String rootPackage) {11 switch (finderType) {12 case NORMAL_TESTS : return new NormalTestFinder(rootPackage);13 case DATA_DRIVEN_TESTS : return new DataDrivenTestFinder(rootPackage);14 default: return new DefaultTestFinder(rootPackage);15 }16 }17}...

Full Screen

Full Screen

Source:NormalTestFinder.java Github

copy

Full Screen

...4import java.util.List;5/**6 * Returns all of the Thucydides classes under the specified package.7 */8public class NormalTestFinder extends TestFinder {9 public NormalTestFinder(final String rootPackage) {10 super(rootPackage);11 }12 @Override13 public List<Class<?>> getClasses() {14 return sorted(new ArrayList(getNormalTestClasses()));15 }16 @Override17 public int countTestMethods() {18 return getAllTestMethods().size();19 }20}...

Full Screen

Full Screen

Source:DefaultTestFinder.java Github

copy

Full Screen

...4import java.util.List;5/**6 * Returns all of the Thucydides classes under the specified package.7 */8public class DefaultTestFinder extends TestFinder {9 public DefaultTestFinder(final String rootPackage) {10 super(rootPackage);11 }12 @Override13 public List<Class<?>> getClasses() {14 return sorted(new ArrayList(getAllTestClasses()));15 }16 @Override17 public int countTestMethods() {18 return getAllTestMethods().size();19 }20}...

Full Screen

Full Screen

Source:TestMethodFinder.java Github

copy

Full Screen

2import java.lang.reflect.Method;3import java.util.List;4import java.util.stream.Collectors;5public class TestMethodFinder {6 private final TestFinder testFinder;7 public TestMethodFinder(TestFinder testFinder) {8 this.testFinder = testFinder;9 }10 public List<Method> withNameContaining(String partialName) {11 return testFinder.getAllTestMethods().stream()12 .filter(testMethod -> testMethod.getName().contains(partialName))13 .collect(Collectors.toList());14 }15}

Full Screen

Full Screen

TestFinder

Using AI Code Generation

copy

Full Screen

1import net.serenitybdd.junit.finder.TestFinder;2import net.thucydides.core.util.EnvironmentVariables;3import net.thucydides.core.util.SystemEnvironmentVariables;4import org.junit.runner.Description;5import org.junit.runner.RunWith;6import org.junit.runner.notification.RunNotifier;7import org.junit.runners.Suite;8import org.junit.runners.model.InitializationError;9import java.io.File;10import java.io.IOException;11import java.util.ArrayList;12import java.util.List;13@RunWith(Suite.class)14public class TestSuiteRunner extends Suite {15 private static final EnvironmentVariables environmentVariables = SystemEnvironmentVariables.createEnvironmentVariables();16 private static final String SERENITY_TEST_ROOT = environmentVariables.getProperty("serenity.test.root", "src/test/java");17 private static final String SERENITY_TEST_SOURCES = environmentVariables.getProperty("serenity.test.sources", "src/test/java");18 private static final String SERENITY_TEST_RESOURCES = environmentVariables.getProperty("serenity.test.resources", "src/test/resources");19 private static final String SERENITY_TEST_CLASSES = environmentVariables.getProperty("serenity.test.classes", "target/test-classes");20 private static final String SERENITY_TEST_OUTPUT = environmentVariables.getProperty("serenity.test.output", "target/site/serenity");21 public TestSuiteRunner(Class<?> klass) throws InitializationError {22 super(klass, getClasses());23 }24 private static List<Class<?>> getClasses() throws InitializationError {25 List<Class<?>> classes = new ArrayList<>();26 try {27 List<Description> tests = new TestFinder().findTestsInDirectory(new File(SERENITY_TEST_ROOT));28 for (Description test : tests) {29 classes.add(Class.forName(test.getClassName()));30 }31 } catch (IOException | ClassNotFoundException e) {32 e.printStackTrace();33 throw new InitializationError(e);34 }35 return classes;36 }37 public void run(RunNotifier notifier) {38 super.run(notifier);39 }40}

Full Screen

Full Screen

TestFinder

Using AI Code Generation

copy

Full Screen

1import net.serenitybdd.junit.finder.TestFinder;2import net.serenitybdd.junit.finder.TestFinderException;3import java.io.File;4import java.util.List;5public class TestFinderExample {6 public static void main(String[] args) throws TestFinderException {7 TestFinder finder = new TestFinder();8 List<File> testClasses = finder.findTestClassesInDirectory("src/test/java");9 System.out.println(testClasses);10 }11}

Full Screen

Full Screen

TestFinder

Using AI Code Generation

copy

Full Screen

1import net.serenitybdd.junit.finder.TestFinder;2import java.io.File;3import java.util.List;4import java.util.stream.Collectors;5import java.util.stream.Stream;6public class TestFinderExample {7 public static void main(String[] args) {8 TestFinder testFinder = new TestFinder();9 List<File> testClasses = testFinder.findTestClassesInDirectory(new File("src/test/java"));10 List<String> testClassNames = testClasses.stream()11 .map(File::getAbsolutePath)12 .map(path -> path.substring(path.indexOf("src")))13 .map(path -> path.replaceAll("/", "."))14 .map(path -> path.substring(0, path.length() - 5))15 .collect(Collectors.toList());16 System.out.println(testClassNames);17 }18}19import net.serenitybdd.junit.finder.TestFinder;20import java.io.File;21import java.util.List;22import java.util.stream.Collectors;23import java.util.stream.Stream;24public class TestFinderExample {25 public static void main(String[] args) {26 TestFinder testFinder = new TestFinder();27 List<File> testClasses = testFinder.findTestClassesInDirectory(new File("src/test/java"));28 List<String> testClassNames = testClasses.stream()29 .map(File::getAbsolutePath)30 .map(path -> path.substring(path.indexOf("src")))31 .map(path -> path.replaceAll("/", "."))32 .map(path -> path.substring(0, path.length() - 5))33 .collect(Collectors.toList());34 System.out.println(testClassNames);35 }36}

Full Screen

Full Screen

TestFinder

Using AI Code Generation

copy

Full Screen

1import net.serenitybdd.junit.finder.TestFinder;2import net.thucydides.core.ThucydidesSystemProperty;3public class TestFinderExample {4 public static void main(String[] args) {5 TestFinder finder = new TestFinder();6 List<Class<?>> tests = finder.findTests("src/test/java/com/serenitybdd/junit/finder");7 tests.forEach(System.out::println);8 }9}

Full Screen

Full Screen

TestFinder

Using AI Code Generation

copy

Full Screen

1TestFinder finder = new TestFinder();2List<TestDescriptor> tests = finder.findTestsInClass("com.test.TestClass");3TestFinder finder = new TestFinder();4List<TestDescriptor> tests = finder.findTestsInClass("com.test.TestClass");5TestFinder finder = new TestFinder();6List<TestDescriptor> tests = finder.findTestsInClass("com.test.TestClass");7TestFinder finder = new TestFinder();8List<TestDescriptor> tests = finder.findTestsInClass("com.test.TestClass");9TestFinder finder = new TestFinder();10List<TestDescriptor> tests = finder.findTestsInClass("com.test.TestClass");11TestFinder finder = new TestFinder();12List<TestDescriptor> tests = finder.findTestsInClass("com.test.TestClass");13TestFinder finder = new TestFinder();14List<TestDescriptor> tests = finder.findTestsInClass("com.test.TestClass");15TestFinder finder = new TestFinder();16List<TestDescriptor> tests = finder.findTestsInClass("com.test.TestClass");17TestFinder finder = new TestFinder();18List<TestDescriptor> tests = finder.findTestsInClass("com.test.TestClass");19TestFinder finder = new TestFinder();20List<TestDescriptor> tests = finder.findTestsInClass("com.test.TestClass");21TestFinder finder = new TestFinder();22List<TestDescriptor> tests = finder.findTestsInClass("com.test.TestClass");23TestFinder finder = new TestFinder();24List<TestDescriptor> tests = finder.findTestsInClass("com.test.TestClass");

Full Screen

Full Screen

TestFinder

Using AI Code Generation

copy

Full Screen

1import net.serenitybdd.junit.finder.TestFinder;2import net.serenitybdd.junit.runners.SerenityRunner;3import org.junit.runner.RunWith;4import org.junit.runners.model.InitializationError;5import java.util.List;6public class RunAllTests {7 public static void main(String[] args) throws InitializationError {8 List<Class<?>> classes = TestFinder.findTestsInPackage("com.mycompany.myproject.tests");9 SerenityRunner runner = new SerenityRunner(classes.toArray(new Class[classes.size()]));10 runner.run();11 }12}

Full Screen

Full Screen

TestFinder

Using AI Code Generation

copy

Full Screen

1TestFinder finder = new TestFinder()2List<String> testClasses = finder.findTestClassesForPackage("com.mycompany.tests")3Serenity also provides a TestFilter class that can be used to filter the test classes found by the TestFinder class. For example, to find all the test classes in a package that are annotated with @RunWith(SerenityRunner.class) and that have a name ending with “Test”:4List<String> testClasses = finder.findTestClassesForPackage("com.mycompany.tests", new TestFilter().withRunner(SerenityRunner.class).and().withNameMatching(".*Test"))5Method Description withRunner(Class<? extends Runner> runner) Filters the test classes to include only those that are annotated with @RunWith(runner) withNameMatching(String regex) Filters the test classes to include only those whose name matches the given regular expression withNameNotMatching(String regex) Filters the test classes to include only those whose name does not match the given regular expression withTags(String... tags) Filters the test classes to include only those that are annotated with @WithTag(tags) withTagsNot(String... tags) Filters the test classes to include only those that are not annotated with @WithTag(tags) withTagsAnyOf(String... tags) Filters the test classes to include only those that are annotated with @WithTag(tags) withTagsNoneOf(String... tags) Filters the test classes to include only those that are not annotated with @WithTag(tags)6For example, to find all the test classes in a package that are annotated with @RunWith(SerenityRunner.class) and that have a name ending with “Test” and that are annotated with @WithTag(“smoke”):7List<String> testClasses = finder.findTestClassesForPackage("com.mycompany.tests", new TestFilter().withRunner(SerenityRunner.class).and

Full Screen

Full Screen

TestFinder

Using AI Code Generation

copy

Full Screen

1import net.serenitybdd.junit.finder.TestFinder2def testClasses = TestFinder.findTests()3println "Number of tests: " + testClasses.size()4def totalStartTime = System.currentTimeMillis()5testClasses.each { testClass ->6def startTime = System.currentTimeMillis()7def testInstance = testClass.newInstance()8testInstance.run()9def endTime = System.currentTimeMillis()10println "Test duration: " + (endTime - startTime) + " ms"11}12def totalEndTime = System.currentTimeMillis()13println "Total duration: " + (totalEndTime - totalStartTime) + " ms"

Full Screen

Full Screen
copy
1class SessionLogAppender implements Appender<ILogEvent> {2 private static final TheadLocal<Object> sessionHolder = new ThreadLocal<Object>();34 private Map<Object, SessionLog> sessionLogs = new ConcurrentHashMap<>();56 /** must be invoked when a new session begins */7 public static void begin(Object session) {8 sessionHolder.set(session);9 }1011 /** must be invoked when a session ends */12 public static void end() {13 Object session = sessionHolder.get();14 writeIfNecessary(sessionLogs.get(session));15 sessionLogs.remove(session);1617 sessionHolder.clear();18 }1920 @Override21 public void doAppend(ILogEvent e) {22 Object session = sessionHolder.get();23 SessionLog l = sessionLogs.get(session);24 if (l == null) {25 l = new SessionLog();26 sessionLogs.put(session, l);27 }28 l.append(e);29 }30}31
Full Screen
copy
1class A extends B { ... }2class B extends ElsAbstractCrudClass<Person> { ... }3
Full Screen
copy
1ContainerSubclass extends SessionLogger {}2
Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Serenity JUnit automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful