Which method of the File class is used to test whether the application can read the file specified?
Table of ContentsIntroductionTestNG is a testing framework designed to simplify a broad range of testing needs, from unit testing (testing a class in isolation of the others) to integration testing (testing entire systems made of several classes, several packages and even several external frameworks, such as application servers).Writing a test is typically a three-step process: Show
The concepts used in this documentation are as follows:
The rest of this manual will explain the following:
AnnotationsHere is a quick overview of the annotations available in TestNG along with their attributes.
testng.xmlYou can invoke TestNG in several different ways:
This section describes the format of testng.xml (you will find documentation on ant and the command line below). The current DTD for testng.xml can be found on the main Web site: testng-1.0.dtd (for your convenience, you might prefer to browse the HTML version). Here is an example testng.xml file:testng.xmltestng.xmlIn this example, TestNG will look at all the classes in the package test.sample and will retain only classes that have TestNG annotations. You can also specify groups and methods to be included and excluded:testng.xmlYou can also define new groups inside testng.xml and specify additional details in attributes, such as whether to run the tests in parallel, how many threads to use, whether you are running JUnit tests, etc... By default, TestNG will run your tests in the order they are found in the XML file. If you want the classes and methods listed in this file to be run in an unpredictable order, set the preserve-order attribute to false testng.xmlPlease see the DTD for a complete list of the features, or read on. Running TestNGTestNG can be invoked in different ways:
Assuming that you have TestNG in your class path, the simplest way to invoke TestNG is as follows: java org.testng.TestNG testng1.xml [testng2.xml testng3.xml ...] You need to specify at least one XML file describing the TestNG suite you are trying to run. Additionally, the following command-line switches are available: Command Line Parameters
This documentation can be obtained by invoking TestNG without any arguments. You can also put the command line switches in a text file, say c:\command.txt, and tell TestNG to use that file to retrieve its parameters: C:> more c:\command.txt -d test-output testng.xml C:> java org.testng.TestNG @c:\command.txtAdditionally, TestNG can be passed properties on the command line of the Java Virtual Machine, for example java -Dtestng.test.classpath="c:/build;c:/java/classes;" org.testng.TestNG testng.xml Here are the properties that TestNG understands: System properties
Example:java org.testng.TestNG -groups windows,linux -testclass org.test.MyTest The ant task and testng.xml allow you to launch TestNG with more parameters (methods to include, specifying parameters, etc...), so you should consider using the command line only when you are trying to learn about TestNG and you want to get up and running quickly. Important: The command line flags that specify what tests should be run will be ignored if you also specify a testng.xml file, with the exception of -includedgroups and -excludedgroups, which will override all the group inclusions/exclusions found in testng.xml. Test methods, Test classes and Test groupsTest methodsTest methods are annotated with @Test. Methods annotated with @Test that happen to return a value will be ignored, unless you set allow-return-values to true in your testng.xml:Test groupsTestNG allows you to perform sophisticated groupings of test methods. Not only can you declare that methods belong to groups, but you can also specify groups that contain other groups. Then TestNG can be invoked and asked to include a certain set of groups (or regular expressions) while excluding another set. This gives you maximum flexibility in how you partition your tests and doesn't require you to recompile anything if you want to run two different sets of tests back to back. Groups are specified in your testng.xml file and can be found either under the For example, it is quite common to have at least two categories of tests
Test1.javapublic class Test1 { @Test(groups = { "functest", "checkintest" }) public void testMethod1() { } @Test(groups = {"functest", "checkintest"} ) public void testMethod2() { } @Test(groups = { "functest" }) public void testMethod3() { } } Invoking TestNG withtestng.xmlwill run all the test methods in that classes, while invoking it with checkintest will only run testMethod1() and testMethod2(). Here is another example, using regular expressions this time. Assume that some of your test methods should not be run on Linux, your test would look like:Test1.java@Test public class Test1 { @Test(groups = { "windows.checkintest" }) public void testWindowsOnly() { } @Test(groups = {"linux.checkintest"} ) public void testLinuxOnly() { } @Test(groups = { "windows.functest" ) public void testWindowsToo() { } } You could use the following testng.xml to launch only the Windows methods:testng.xmlNote: TestNG uses regular expressions, and not wildmats. Be aware of the difference (for example, "anything" is matched by ".*" -- dot star -- and not "*"). Method groupsYou can also exclude or include individual methods:testng.xmlGroups of groupsGroups can also include other groups. These groups are called "MetaGroups". For example, you might want to define a group "all" that includes "checkintest" and "functest". "functest" itself will contain the groups "windows" and "linux" while "checkintest will only contain "windows". Here is how you would define this in your property file:testng.xmlExclusion groupsTestNG allows you to include groups as well as exclude them. For example, it is quite usual to have tests that temporarily break because of a recent change, and you don't have time to fix the breakage yet. 4 However, you do want to have clean runs of your functional tests, so you need to deactivate these tests but keep in mind they will need to be reactivated.A simple way to solve this problem is to create a group called "broken" and make these test methods belong to it. For example, in the above example, I know that testMethod2() is now broken so I want to disable it: Java@Test(groups = {"checkintest", "broken"} ) public void testMethod2() { } All I need to do now is to exclude this group from the run:testng.xmlThis way, I will get a clean test run while keeping track of what tests are broken and need to be fixed later.
Partial groupsYou can define groups at the class level and then add groups at the method level:All.java@Test(groups = { "checkin-test" }) public class All { @Test(groups = { "func-test" ) public void method1() { ... } public void method2() { ... } } In this class, method2() is part of the group "checkin-test", which is defined at the class level, while method1() belongs to both "checkin-test" and "func-test".ParametersTest methods don't have to be parameterless. You can use an arbitrary number of parameters on each of your test method, and you instruct TestNG to pass you the correct parameters with the @Parameters annotation. There are two ways to set these parameters: with testng.xml or programmatically. Parameters from testng.xmlIf you are using simple values for your parameters, you can specify them in your testng.xml:Java@Parameters({ "first-name" }) @Test public void testSingleString(String firstName) { System.out.println("Invoked testString " + firstName); assert "Cedric".equals(firstName); } In this code, we specify that the parameter firstName of your Java method should receive the value of the XML parameter called first-name. This XML parameter is defined in testng.xml:testng.xmlThe same technique can be used for @Before/After and @Factory annotations:@Parameters({ "datasource", "jdbcDriver" }) @BeforeMethod public void beforeTest(String ds, String driver) { m_dataSource = ...; // look up the value of datasource m_jdbcDriver = driver; } This time, the two Java parameter ds and driver will receive the value given to the properties datasource and jdbc-driver respectively.Parameters can be declared optional with the Optional annotation: @Parameters("db") @Test public void testNonExistentParameter(@Optional("mysql") String db) { ... } If no parameter named "db" is found in your testng.xml file, your test method will receive the default value specified inside the @Optional annotation: "mysql".The @Parameters annotation can be placed at the following locations:
Parameters with DataProvidersSpecifying parameters in testng.xml might not be sufficient if you need to pass complex parameters, or parameters that need to be created from Java (complex objects, objects read from a property file or a database, etc...). In this case, you can use a Data Provider to supply the values you need to test. A Data Provider is a method on your class that returns an array of array of objects. This method is annotated with @DataProvider: Java//This method will provide data to any test method that declares that its Data Provider //is named "test1" @DataProvider(name = "test1") public Object[][] createData1() { return new Object[][] { { "Cedric", new Integer(36) }, { "Anne", new Integer(37)}, }; } //This test method declares that its data should be supplied by the Data Provider //named "test1" @Test(dataProvider = "test1") public void verifyData1(String n1, Integer n2) { System.out.println(n1 + " " + n2); } will print Cedric 36 Anne 37 A @Test method specifies its Data Provider with the dataProvider attribute. This name must correspond to a method on the same class annotated with @DataProvider(name="...") with a matching name.By default, the data provider will be looked for in the current test class or one of its base classes. If you want to put your data provider in a different class, it needs to be a static method or a class with a non-arg constructor, and you specify the class where it can be found in the dataProviderClass attribute: StaticProvider.javapublic class StaticProvider { @DataProvider(name = "create") public static Object[][] createData() { return new Object[][] { new Object[] { new Integer(42) } }; } } public class MyTest { @Test(dataProvider = "create", dataProviderClass = StaticProvider.class) public void test(Integer n) { // ... } } The data provider supports injection too. TestNG will use the test context for the injection. The Data Provider method can return one of the following types:
For example, the following code prints the name of the test method inside its @DataProvider: @DataProvider(name = "dp") public Object[][] createData(Method m) { System.out.println(m.getName()); // print test method name return new Object[][] { new Object[] { "Cedric" }}; } @Test(dataProvider = "dp") public void test1(String s) { } @Test(dataProvider = "dp") public void test2(String s) { } and will therefore display: test1 test2 Data providers can run in parallel with the attribute parallel: @DataProvider(parallel = true) // ... Parallel data providers running from an XML file share the same pool of threads, which has a size of 10 by default. You can modify this value in theParameters in reportsParameters used to invoke your test methods are shown in the HTML reports generated by TestNG. Here is an example:
DependenciesSometimes, you need your test methods to be invoked in a certain order. Here are a few examples:
Dependencies with annotationsYou can use the attributes dependsOnMethods or dependsOnGroups, found on the @Test annotation. There are two kinds of dependencies:
In this example, method1() is declared as depending on method serverStartedOk(), which guarantees that serverStartedOk() will always be invoked first. You can also have methods that depend on entire groups: @Test(groups = { "init" }) public void serverStartedOk() {} @Test(groups = { "init" }) public void initEnvironment() {} @Test(dependsOnGroups = { "init.*" }) public void method1() {}In this example, method1() is declared as depending on any group matching the regular expression "init.*", which guarantees that the methods serverStartedOk() and initEnvironment() will always be invoked before method1().
If a method depended upon fails and you have a hard dependency on it (alwaysRun=false, which is the default), the methods that depend on it are not marked as FAIL but as SKIP. Skipped methods will be reported as such in the final report (in a color that is neither red nor green in HTML), which is important since skipped methods are not necessarily failures. Both dependsOnGroups and dependsOnMethods accept regular expressions as parameters. For dependsOnMethods, if you are depending on a method which happens to have several overloaded versions, all the overloaded methods will be invoked. If you only want to invoke one of the overloaded methods, you should use dependsOnGroups. For a more advanced example of dependent methods, please refer to this article, which uses inheritance to provide an elegant solution to the problem of multiple dependencies. By default, dependent methods are grouped by class. For example, if method b() depends on method a() and you have several instances of the class that contains these methods (because of a factory of a data provider), then the invocation order will be as follows: a(1) a(2) b(2) b(2) TestNG will not run b() until all the instances have invoked their a() method.This behavior might not be desirable in certain scenarios, such as for example testing a sign in and sign out of a web browser for various countries. In such a case, you would like the following ordering: signIn("us") signOut("us") signIn("uk") signOut("uk") For this ordering, you can use the XML attribute group-by-instances. This attribute is valid either onDependencies in XMLAlternatively, you can specify your group dependencies in the testng.xml file. You use theFactoriesFactories allow you to create tests dynamically. For example, imagine you want to create a test method that will access a page on a Web site several times, and you want to invoke it with different values:TestWebServer.javapublic class TestWebServer { @Test(parameters = { "number-of-times" }) public void accessPage(int numberOfTimes) { while (numberOfTimes-- > 0) { // access the web page } } }testng.xmlWebTestFactory.javapublic class WebTestFactory { @Factory public Object[] createInstances() { Object[] result = new Object[10]; for (int i = 0; i < 10; i++) { result[i] = new WebTest(i * 10); } return result; } } and the new test class is now:WebTest.javapublic class WebTest { private int m_numberOfTimes; public WebTest(int numberOfTimes) { m_numberOfTimes = numberOfTimes; } @Test public void testServer() { for (int i = 0; i < m_numberOfTimes; i++) { // access the web page } } }Your testng.xml only needs to reference the class that contains the factory method, since the test instances themselves will be created at runtime: Or, if building a test suite instance programatically, you can add the factory in the same manner as for tests: TestNG testNG = new TestNG(); testNG.setTestClasses(WebTestFactory.class); testNG.run();The factory method can receive parameters just like @Test and @Before/After and it must return Object[]. The objects returned can be of any class (not necessarily the same class as the factory class) and they don't even need to contain TestNG annotations (in which case they will be ignored by TestNG). Factories can also be used with data providers, and you can leverage this functionality by putting the @Factory annotation either on a regular method or on a constructor. Here is an example of a constructor factory: @Factory(dataProvider = "dp") public FactoryDataProviderSampleTest(int n) { super(n); } @DataProvider static public Object[][] dp() { return new Object[][] { new Object[] { 41 }, new Object[] { 42 }, }; } The example will make TestNG create two test classes, on with the constructor invoked with the value 41 and the other with 42.Class level annotationsThe @Test annotation can be put on a class instead of a test method:Test1.java@Test public class Test1 { public void test1() { } public void test2() { } } The effect of a class level @Test annotation is to make all the public methods of this class to become test methods even if they are not annotated. You can still repeat the @Test annotation on a method if you want to add certain attributes.For example: Test1.java@Test public class Test1 { public void test1() { } @Test(groups = "g1") public void test2() { } } will make both test1() and test2() test methods but on top of that, test2() now belongs to the group "g1".Ignoring testsTestNG lets you ignore all the @Test methods :
When used at the method level @Ignore annotation is functionally equivalent to @Test(enabled=false). Here's a sample that shows how to ignore all tests within a class. TestcaseSample.javaimport org.testng.annotations.Ignore; import org.testng.annotations.Test; @Ignore public class TestcaseSample { @Test public void testMethod1() { } @Test public void testMethod2() { } } The @Ignore annotation has a higher priority than individual @Test method annotations. When @Ignore is placed on a class, all the tests in that class will be disabled. To ignore all tests in a particular package, you just need to create package-info.java and add the @Ignore annotation to it. Here's a sample :package-info.java@Ignore package com.testng.master; import org.testng.annotations.Ignore; This causes all the @Test methods to be ignored in the package com.testng.master and all of its sub-packages.Parallelism and time-outsYou can instruct TestNG to run your tests in separate threads in various ways.Parallel suitesThis is useful if you are running several suite files (e.g. "java org.testng.TestNG testng1.xml testng2.xml") and you want each of these suites to be run in a separate thread. You can use the following command line flag to specify the size of a thread pool: java org.testng.TestNG -suitethreadpoolsize 3 testng1.xml testng2.xml testng3.xml The corresponding ant task name is suitethreadpoolsize.Parallel tests, classes and methodsThe parallel attribute on the
Additionally, the attribute thread-count allows you to specify how many threads should be allocated for this execution. You can also specify that a @Test method should be invoked from different threads. You can use the attribute threadPoolSize to achieve this result: @Test(threadPoolSize = 3, invocationCount = 10, timeOut = 10000) public void testServer() { In this example, the function testServer will be invoked ten times from three different threads. Additionally, a time-out of ten seconds guarantees that none of the threads will block on this thread forever. Rerunning failed testsEvery time tests fail in a suite, TestNG creates a file called testng-failed.xml in the output directory. This XML file contains the necessary information to rerun only these methods that failed, allowing you to quickly reproduce the failures without having to run the entirety of your tests. Therefore, a typical session would look like this: java -classpath testng.jar;%CLASSPATH% org.testng.TestNG -d test-outputs testng.xml java -classpath testng.jar;%CLASSPATH% org.testng.TestNG -d test-outputs test-outputs\testng-failed.xmlNote that testng-failed.xml will contain all the necessary dependent methods so that you are guaranteed to run the methods that failed without any SKIP failures. Sometimes, you might want TestNG to automatically retry a test whenever it fails. In those situations, you can use a retry analyzer. When you bind a retry analyzer to a test, TestNG automatically invokes the retry analyzer to determine if TestNG can retry a test case again in an attempt to see if the test that just fails now passes. Here is how you use a retry analyzer:
Javaimport org.testng.IRetryAnalyzer; import org.testng.ITestResult; public class MyRetry implements IRetryAnalyzer { private int retryCount = 0; private static final int maxRetryCount = 3; @Override public boolean retry(ITestResult result) { if (retryCount < maxRetryCount) { retryCount++; return true; } return false; } }Javaimport org.testng.Assert; import org.testng.annotations.Test; public class TestclassSample { @Test(retryAnalyzer = MyRetry.class) public void test2() { Assert.fail(); } }JUnit testsTestNG can run JUnit 3 and JUnit 4 tests. All you need to do is put the JUnit jar file on the classpath, specify your JUnit test classes in the testng.classNames property and set the testng.junit property to true:testng.xmlThe behavior of TestNG in this case is similar to JUnit depending on the JUnit version found on the class path:
Running TestNG programmaticallyYou can invoke TestNG from your own programs very easily:JavaTestListenerAdapter tla = new TestListenerAdapter(); TestNG testng = new TestNG(); testng.setTestClasses(new Class[] { Run2.class }); testng.addListener(tla); testng.run(); This example creates a TestNG object and runs the test class Run2. It also adds a TestListener. You can either use the adapter class org.testng.TestListenerAdapter or implement org.testng.ITestListener yourself. This interface contains various callback methods that let you keep track of when a test starts, succeeds, fails, etc...Similarly, you can invoke TestNG on a testng.xml file or you can create a virtual testng.xml file yourself. In order to do this, you can use the classes found the package org.testng.xml: XmlClass, XmlTest, etc... Each of these classes correspond to their XML tag counterpart. For example, suppose you want to create the following virtual file: Please see the JavaDocs for the entire API. BeanShell and advanced group selectionIf the testng.xmlHere is an example that shows how the suite name is getting altered in runtime: public class AlterSuiteNameListener implements IAlterSuiteListener { @Override public void alter(List
Test resultsSuccess, failure and assertA test is considered successful if it completed without throwing any exception or if it threw an exception that was expected (see the documentation for the expectedExceptions attribute found on the @Test annotation). Your test methods will typically be made of calls that can throw an exception, or of various assertions (using the Java "assert" keyword). An "assert" failing will trigger an AssertionErrorException, which in turn will mark the method as failed (remember to use -ea on the JVM if you are not seeing the assertion errors). Here is an example test method: @Test public void verifyLastName() { assert "Beust".equals(m_lastName) : "Expected name Beust, for" + m_lastName; } TestNG also include JUnit's Assert class, which lets you perform assertions on complex objects: import static org.testng.AssertJUnit.*; //... @Test public void verify() { assertEquals("Beust", m_lastName); }Note that the above code use a static import in order to be able to use the assertEquals method without having to prefix it by its class. Logging and resultsThe results of the test run are created in a file called index.html in the directory specified when launching SuiteRunner. This file points to various other HTML and text files that contain the result of the entire test run.It's very easy to generate your own reports with TestNG with Listeners and Reporters:
Logging ListenersHere is a listener that displays a "." for each passed test, a "F" for each failure and a "S" for each skip: public class DotTestListener extends TestListenerAdapter { private int m_count = 0; @Override public void onTestFailure(ITestResult tr) { log("F"); } @Override public void onTestSkipped(ITestResult tr) { log("S"); } @Override public void onTestSuccess(ITestResult tr) { log("."); } private void log(String string) { System.out.print(string); if (++m_count % 40 == 0) { System.out.println(""); } } } In this example, I chose to extend TestListenerAdapter, which implements ITestListener with empty methods, so I don't have to override other methods from the interface that I have no interest in. You can implement the interface directly if you prefer.Here is how I invoke TestNG to use this new listener: Shelljava -classpath testng.jar;%CLASSPATH% org.testng.TestNG -listener org.testng.reporters.DotTestListener test\testng.xml and the output:Shell........................................ ........................................ ........................................ ........................................ ........................................ ......................... =============================================== TestNG JDK 1.5 Total tests run: 226, Failures: 0, Skips: 0 =============================================== Note that when you use -listener, TestNG will automatically determine the type of listener you want to use.Logging ReportersThe org.testng.IReporter interface only has one method: public void generateReport(ListJUnitReportsTestNG contains a listener that takes the TestNG results and outputs an XML file that can then be fed to JUnitReport. Here is an example, and the ant task to create this report: build.xmlNote: a current incompatibility between the JDK 1.5 and JUnitReports prevents the frame version from working, so you need to specify "noframes" to get this to work for now. Reporter APIIf you need to log messages that should appear in the generated HTML reports, you can use the class org.testng.Reporter: Reporter.log("M3 WAS CALLED");
XML ReportsTestNG offers an XML reporter capturing TestNG specific information that is not available in JUnit reports. This is particularly useful when the user's test environment needs to consume XML results with TestNG-specific data that the JUnit format can't provide. This reporter can be injected into TestNG via the command line with -reporter. Here's a sample usage: -reporter org.testng.reporters.XMLReporter:generateTestResultAttributes=true,generateGroupsAttribute=true. The full set of options that can be passed is detailed in the below table. Make sure to use :
Below is a sample of the output of such a reporter: This reporter is injected along with the other default listeners so you can get this type of output by default. The listener provides some properties that can tweak the reporter to fit your needs. The following table contains a list of these properties with a short explanation:
In order to configure this reporter you can use the -reporter option in the command line or the Ant task with the nested TestNG Exit Codes When TestNG completes execution, it exits with a return code.
YAMLTestNG supports YAML as an alternate way of specifying your suite file. For example, the following XML file:and here is its YAML version: name: SingleSuite threadCount: 4 parameters: { n: 42 } tests: - name: Regression2 parameters: { count: 10 } excludedGroups: [ broken ] classes: - test.listeners.ResultEndMillisTest Here is TestNG's own suite file, and its YAML counterpart.You might find the YAML file format easier to read and to maintain. YAML files are also recognized by the TestNG Eclipse plug-in. You can find more information about YAML and TestNG in this blog post. Note: file: org.yaml snakeyaml 1.23 Or if you were using Gradle, you would add a dependency as below into your build.gradle file: compile group: 'org.yaml', name: 'snakeyaml', version: '1.23'Dry Run for your testsWhen launched in dry run mode, TestNG will display a list of the test methods that would be invoked but without actually calling them.You can enable dry run mode for TestNG by passing the JVM argument -Dtestng.mode.dryrun=true JVM Arguments in TestNG
Back to my home page. Or check out some of my other projects:
Logging framework integration in TestNG Starting from TestNG version 7.5 TestNG makes use of the logging facade provided by Slf4j. What methods are used to read from file?Which of these methods are used to read in from file? Explanation: Each time read() is called, it reads a single byte from the file and returns the byte as an integer value. read() returns -1 when the end of the file is encountered.
Which method is used to test whether the application can modify the file denoted by this abstract pathname?canWrite()
Tests whether the application can modify the file denoted by this abstract pathname.
Which class is used for reading from a file?Java FileReader class is used to read data from the file. It returns data in byte format like FileInputStream class.
Which methods are used to read file in Java?There are several ways to read a plain text file in Java e.g. you can use FileReader, BufferedReader, or Scanner to read a text file. Every utility provides something special e.g. BufferedReader provides buffering of data for fast reading, and Scanner provides parsing ability.
|