@BeforeSuite: The annotated method will be run before all tests in this suite have run. @AfterSuite: The annotated method will be run after all tests in this suite have run. @BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run. @AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the tag have run. @BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked. @AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked. @BeforeClass: The annotated method will be run before the first test method in the current class is invoked. @AfterClass: The annotated method will be run after all the test methods in the current class have been run. @BeforeMethod: The annotated method will be run before each test method. @AfterMethod: The annotated method will be run after each test method.
Behaviour of annotations in superclass of a TestNG class
The annotations above will also be honored (inherited) when placed on a superclass of a TestNG class. This is useful for example to centralize test setup for multiple test classes in a common superclass.
In that case, TestNG guarantees that the «@Before» methods are executed in inheritance order (highest superclass first, then going down the inheritance chain), and the «@After» methods in reverse order (going up the inheritance chain).
testng.xml
You 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.xml
testng.xml
In 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.xml
You 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.xml
Please see the DTD for a complete list of the features, or read on.
Running TestNG
Assuming that you have TestNG in your class path, the simplest way to invoke TestNG is as follows: 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:
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:
Additionally, TestNG can be passed properties on the command line of the Java Virtual Machine, for example Here are the properties that TestNG understands:
System properties
Property
Type
Documentation
testng.test.classpath
A semi-colon separated series of directories that contain your test classes.
If this property is set, TestNG will use it to look for your test classes instead of the class path. This is convenient if you are using the package tag in your XML file and you have a lot of classes in your classpath, most of them not being test classes.
Example: 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 groups
Test methods
Test groups
TestNG 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.
For example, it is quite common to have at least two categories of tests
Test1.java
testng.xml
will 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
testng.xml
Method groups
testng.xml
Groups of groups
testng.xml
Exclusion groups
TestNG 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:
testng.xml
This way, I will get a clean test run while keeping track of what tests are broken and need to be fixed later.
Note: you can also disable tests on an individual basis by using the «enabled» property available on both @Test and @Before/After annotations.
Partial groups
All.java
Parameters
Test 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.xml
testng.xml
The same technique can be used for @Before/After and @Factory annotations:
Parameters can be declared optional with the Optional annotation: 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 DataProviders
Specifying 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:
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.java
For example, the following code prints the name of the test method inside its @DataProvider: and will therefore display: Data providers can run in parallel with the attribute parallel: 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 the tag of your XML file: If you want to run a few specific data providers in a different thread pool, you need to run them from a different XML file.
Parameters in reports
Parameters used to invoke your test methods are shown in the HTML reports generated by TestNG. Here is an example:
Dependencies
Dependencies with annotations
You can use the attributes dependsOnMethods or dependsOnGroups, found on the @Test annotation.
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:
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().
Note: as stated before, the order of invocation for methods that belong in the same group is not guaranteed to be the same across test runs.
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: 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: For this ordering, you can use the XML attribute group-by-instances. This attribute is valid either on or :
Dependencies in XML
Factories
TestWebServer.java
testng.xml
WebTestFactory.java
WebTest.java
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:
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: 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 annotations
Test1.java
Test1.java
Ignoring tests
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.java
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
Parallelism and time-outs
Parallel suites
Parallel tests, classes and methods
Additionally, the attribute thread-count allows you to specify how many threads should be allocated for this execution.
Note: the @Test attribute timeOut works in both parallel and non-parallel mode.
Rerunning failed tests
Note 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.
JUnit tests
testng.xml
The behavior of TestNG in this case is similar to JUnit depending on the JUnit version found on the class path:
Running TestNG programmatically
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: You would use the following code: And then you can pass this XmlSuite to TestNG:
Requirement for Proof of Negative COVID-19 Test or Documentation of Recovery from COVID-19
Required for all air passengers two years of age or older boarding a flight from a foreign country to the United States
You are considered fully vaccinated:
If you don’t meet these requirements, you are NOT fully vaccinated.
* CDC has not recommended the use of mix-and-match COVID-19 vaccine primary series. However, such strategies are increasingly common in many countries outside of the United States. Therefore, for the of purpose of interpreting vaccination records for travel to the United States, CDC will accept combinations of accepted COVID-19 vaccines.
See Requirement for Proof of COVID-19 Vaccination for Air Passengers for more information about necessary vaccination documentation.
COVID-19 Testing Requirement for International Travel to the United States
On October 25, 2021 CDC amended its January 25, 2021 Order, titled, “Requirement for Proof of NegativeCOVID-19 Test or Recovery from COVID-19 for All Air Passengers Arriving in the United States.” This amendment updates COVID-19 testing requirements for air passengers 2 years or older boarding a flight to the United States, depending on COVID-19 vaccination status.
Effective November 8, 2021 at 12:01am EST (5:01am GMT), before boarding a flight to the US from a foreign country, all air passengers- 2 years or older – are required to present a negative COVID-19 viral test result, within a time period based on their vaccination status (see table below), or present documentation of having recovered from COVID-19 in the last 90 days. Air passengers will also be required to confirm in the form of an attestation that the information they present is true.
Type of Air Passenger (2 years or older)
Number of Days to get a COVID-19 Viral Test Before a Flight’s Departure to the US
Time Period for Testing for Air Passengers to Obtain a Negative COVID-19 Viral Test
Fully vaccinated
No more than 3 days
Not fully vaccinated
No more than 1 day
For the full list of requirements and exemptions, please review the language in the Order.
International travel poses additional risks and even fully vaccinated travelers are at increased risk for getting and possibly spreading new COVID-19 variants.
CDC recommends delaying international travel until you are fully vaccinated.
Aircraft Operators/ Airlines/ Crew
For additional information, resources, and FAQs please visit the following webpages:
Frequently Asked Questions
Overview
Yes, this Order applies to all air passengers 2 years or older traveling into the US, including US citizens and lawful permanent residents (Green Card holders) unless exempted.
Yes, at this time all air passengers, 2 years or older, traveling to the US, regardless of vaccination or antibody status, are required to provide a negative COVID-19 viral test result or documentation of recovery unless exempted.
No, the requirements of this Order only apply to air travel into the U.S.
No, the requirement to present a negative result of a COVID-19 viral test or documentation recovery from COVID-19 does not apply to air passengers with flights from a US territory to a US state.
U.S. territories include American Samoa, Guam, the Northern Mariana Islands, the Commonwealth of Puerto Rico, and the U.S. Virgin Islands.
Diplomats and special visa holders are not exempt from this Order.
Federal testing requirements must be met to board a plane to the US. Some state and local governments may have similar or more restrictive testing requirements for air passengers arriving in their jurisdictions. Always check and follow state and local recommendations or requirements related to travel in addition to federal requirements.
Test and Vaccination Documentation Requirements
You must be tested with a viral test that could be either an antigen test or a nucleic acid amplification test (NAAT). Examples of available NAATs for SARS-CoV-2 include but are not restricted to reverse transcription polymerase chain reaction (RT-PCR), reverse transcription loop-mediated isothermal amplification (RT-LAMP), transcription-mediated amplification (TMA), nicking enzyme amplification reaction (NEAR), and helicase-dependent amplification (HDA). The test used must be authorized for use by the relevant national authority for the detection of SARS-CoV-2 in the country where the test is administered. A viral test conducted for U.S. Department of Defense (DOD) personnel, including DOD contractors, dependents, and other U.S. government employees, and tested by a DOD laboratory located in a foreign country also meets the requirements of the Order.
Rapid tests are acceptable if they are a viral test that meet the requirements under the Order.
You can use a self-test (sometimes referred to as home test) that meets the following criteria:
Some countries may restrict importation of tests that are not authorized or registered there. If you are considering bringing a U.S.-authorized test with you for use outside of the United States, contact authorities at your destination for information before you travel.
A test result must be in the form of written documentation (paper or digital copy). The documentation must include:
Before boarding a flight to the U.S., you will need to show a paper or digital copy of your test result for review by the airline and for review upon request by public health officials after you arrive in the U.S.
See Requirement for Proof of COVID-19 Vaccination for Air Passengers for information about necessary vaccination documentation.
People who have recovered from COVID-19 can continue to test positive for up to 3 months after their infection. CDC does not recommend retesting within 3 months after a person with COVID-19 first developed symptoms of COVID-19 (or the date their sample was taken for their first positive viral diagnostic test if their infection was asymptomatic).
If you have had a positive viral test on a sample taken during the past 90 days, and you have met the criteria to end isolation, you may travel instead with your positive viral test results and a signed letter from a licensed healthcare provider or a public health official that states you have been cleared for travel. The positive test result and letter together are referred to as “documentation of recovery.”
A letter from your healthcare provider or a public health official that clears you to end isolation, e.g., to return to work or school, can be used to show you are cleared to travel, even if travel isn’t specifically mentioned in the letter. The letter must have information that identifies you personally (e.g., name and date of birth) that matches the personal identifiers on your passport or other travel documents. The letter must be signed and dated on official letterhead that contains the name, address, and phone number of the healthcare provider or public health official who signed the letter.
If you have recovered from COVID-19 but are not able to obtain documentation of recovery that fulfills the requirements, you will need to show a negative COVID-19 viral test result from a sample taken no more than 3 days (or 1 day if you are not fully vaccinated) before your flight to the US departs.
Even if you have recovered from COVID-19, if you develop symptoms of COVID-19 you should isolate, not travel, and consult with a healthcare provider for testing recommendations.
Airlines and other aircraft operators must be able to confirm the test result and proof of vaccination and review other required information and should determine when translation is necessary for these purposes. If your documents are in a language other than English, you should check with your airline or aircraft operator before travel.
An attestation is a statement, writing, entry, or other representation under 18 U.S.C. § 1001 that confirms that the information provided is true. Willingly providing false or misleading information may lead to fines and other criminal penalties.
As required by United States federal law, all airlines or other aircraft operators will provide and collect the passenger attestation on behalf of the U.S. Government prior to boarding.
You are required to retain a paper or digital copy of your negative test result or documentation of recovery for the entirety of your itinerary as federal public health officials may request to see these documents at the port of entry. State, territorial, tribal and/or local health departments in the United States may also request them under their own public health authorities.
The attestation should be submitted to and retained by the airline or aircraft operator.
CDC is not able to reimburse you for COVID-19 testing fees. You may wish to contact your insurance provider or the location that provided your test about payment options.
Timing Requirements
If you are fully vaccinated and traveling with proof of vaccination, you must get tested no more than 3 days before your flight to the U.S. departs. If you are not fully vaccinated and 2 years and older, you must get tested no more than 1 day before your flight to the US departs.
Recent CDC modeling shows that for travelers who are not fully vaccinated, getting a viral test 1 day prior to departure can substantially reduce the chance of traveling with COVID-19. Testing 1 day before departure for travelers who are not fully vaccinated reduces the risk of transmission during travel and of importing additional COVID-19 cases into the US. For travelers who are fully vaccinated, the combination of vaccination and pre-travel testing provides a greater level of public health protection than either measure alone and is consistent with a layered strategy.
The 1-day period is 1 day before the flight’s departure and the 3-day period is the 3 days before the flight’s departure. The Order uses 1-day and 3-day time frames instead of 24 hours and 72 hours to provide more flexibility to the air passenger and aircraft operator. By using a 1-day and 3-day window, test acceptability does not depend on the time of the flight or the time of day that the test sample was taken.
For example, if you are fully vaccinated and your flight is at 1pm on a Friday, you could board with a negative test that was taken any time on the prior Tuesday or after. If you are not fully vaccinated and your flight is at 1pm on a Friday, you could board with a negative test that was taken any time on the prior Thursday.
Children between the ages of 2 and 17 who are not fully vaccinated may board a flight to the United States with a negative pre-departure COVID-19 viral test conducted on a specimen collected no more than 3 days before departure (i.e., Qualifying Test for Fully Vaccinated) if traveling accompanied by fully vaccinated parents or guardians.
If traveling unaccompanied or if one or more of the parents or guardians accompanying the child is not fully vaccinated, the child must present a negative pre-departure COVID-19 viral test on a specimen collected no more than 1 day before departure. While children under 2 years of age are not required to get a test, CDC recommends a pre-departure test for these children whenever possible.
If a trip is shorter than 1 day or 3 days, a viral test taken in the United States can be used to fulfill the requirements of the Order as long as the specimen was taken no more than 1 day or 3 days, depending on your vaccination status, before your return flight to the U.S. departs. If your return travel is delayed longer than 1 day or 3 days after the test, you will need to be retested before your return flight.
If you are considering this option, you should additionally consider, as a contingency when making your travel plans, the availability of testing capacity at your destination and the time frame needed to obtain results.
When making plans for travel, you should consider the availability of testing capacity at your destination and the time frame needed to obtain results.
You may consider contacting the airline regarding options for changing your departure date to allow time for a test, see if the airline has identified options for testing, or if there are options available for changing their flights to transit through a location where you can get tested before boarding your final flight to the United States.
Air passengers traveling to the US are required to present a negative COVID-19 test result or documentation of recovery. Airlines must confirm the negative test result or documentation of recovery for all passengers before boarding. If you choose not to present a test result or documentation of recovery, the airline must not allow you to board.
Positive Test or COVID-19 Exposure
You should self-isolate and delay your travel if you develop symptoms or your pre-departure test result is positive until you have recovered from COVID-19. Airlines must refuse to board anyone who does not present a negative test result for COVID-19 or documentation of recovery.
If you know or suspect that you have COVID-19, you should self-isolate and NOT travel until you have met CDC’s criteria for discontinuing isolation.
If you are not fully vaccinated and have had close contact with a person with COVID-19 (i.e., who are considered exposed to COVID-19), you should self-quarantine and NOT travel until you have met criteria for discontinuing quarantine. If you are fully vaccinated, or have documentation of recovery from COVID-19 in the past 90 days, you do not have to self-quarantine after exposure to a person with COVID-19 and can travel unless you have COVID-19 symptoms.
Connecting or Delayed Flights
If you booked an itinerary from a U.S. state or territory to another U.S. state or territory and the itinerary has you taking a connecting flight through a foreign country, CDC does not require that you be tested. An example of this situation is an itinerary booked between the Northern Mariana Islands (a U.S. territory) and the U.S. mainland via Japan.
However, some U.S. state and local governments and foreign governments may have their own testing requirements for air passengers arriving in their jurisdictions that may differ from U.S. federal requirements. Always check and follow recommendations or requirements related to travel to your destination in addition to U.S. federal requirements.
Yes. Any flight entering the U.S. from a foreign country, even for a connection, will require testing before departure.
If your itinerary has you arriving to the US via one or more connecting flights, your test can be taken within 1 day or 3 days, depending on your vaccination status, before the departure of the first flight.
You also have the option of getting tested en route during one of your connections. However, you should consider where in the connecting airport testing is available and if you would be able to access it while in transit. If you choose this strategy and are unable to get a test en route, you will not be able to board your flight to the United States. You should also be aware that if you test positive en route, you will not be allowed to continue your travel and may need to stay at that location until you end isolation.
Please note, if you planned an itinerary incorporating one or more overnight stays en route to the US, you will need to make sure your test is not expired before your flight that will enter the US. You do not need to be retested if the itinerary requires an overnight connection because of limitations in flight availability.
If the firstflight in your trip is delayed past the 1-day or 3-day limit of testing due to a situation outside of your control (e.g., delays because of severe weather or aircraft mechanical problem), and that delay is 24 hours or less past the 1-day or 3-day limit for testing, you do not need to be retested. If the delay is more than 24 hours past the 1- day or 3-day limit, then you will need to be retested.
If a connecting flight in your trip is delayed past the 1-day or 3-day limit of testing due to a situation outside of your control (e.g., delays because of severe weather or aircraft mechanical problem), and that delay is less than 48 hours past the 1-day or 3-day limit for testing, you do not need to be retested. If the delay is more than 48 hours past the 1-day or 3-day limit, then you will need to be retested.
CDC does not reimburse and is unable to help travelers get reimbursements for travel expenses because of canceled or delayed travel due to COVID-19 or testing requirements for air passengers flying to the US. While some companies may base their policies on CDC’s travel recommendations or requirements, each company establishes its own refund policies.
In some cases, trip cancellation insurance can protect your financial investment in a trip if you need to change your itinerary in the event of an international outbreak. Visit CDC’s Travelers’ Health website if you would like to learn more about travel insurance, including trip cancellation insurance.
Exemptions
Exemptions may be granted on an extremely limited basis when emergency travel (like an emergency medical evacuation) must occur to preserve someone’s life, health against a serious danger, or physical safety and testing cannot be completed before travel.
CDC may grant a humanitarian exemption in very limited circumstances only when an individual must travel to the United States to preserve health (e.g., emergency medical evacuations) or safety (e.g., violence) and is unable to access or complete the testing requirement before travel.
Individuals who fit the exemption criteria described in CDC’s Order may contact the U.S. embassy or consulate in or nearest the country from which they are departing for the United States. The embassy will then transmit this information to the CDC for consideration.
Please review the procedures for applying for a humanitarian exemption as listed on the webpage of the embassy or consulate where you will apply. This link will lead you to the relevant embassy or consulate: https://www.usembassy.gov external icon
To facilitate the review of a humanitarian exemption request, individuals should submit the following information to the embassy or consulate for transmission to the CDC. All information needs to be completed in full and in English for the request to be sent to CDC.
Private flights or general aviation aircraft may transport a patient who has tested positive, or exposed contacts if they adhere to CDC’s Interim Guidance for Transporting or Arranging Transportation by Air into, from, or within the United States of People with COVID-19 or COVID-19 Exposure.