根据以下文件:http://testng.org/doc/documentation-main.html
“如果测试类上有方法tearDown(),则会在每个测试方法之后调用它”(JUnit 3)
虽然措辞有点含糊不清,但我得到了一些奇特的结果。
我正在使用JUnit版本:3.8.1
这是在我的测试之前运行的,但我希望它会在之后。测试后它没有运行:
@Test(groups = { "all", "inspections" })
public void tearDown() {
debug.print(thisClass + " ViewAssetInspectionsTest - tearDown invoked.");
boolean isHomeDisplayed = loggedInPage.isHomeDisplayed();
if (isHomeDisplayed) {
boolean clickBackArrow = loggedInPage.goBack();
Common.reportAssert(driver, thisClass, " click Back Arrow?", clickBackArrow);
}
boolean isUnitPropertiesToggleActive = loggedInPage.isUnitPropertiesToggleActive();
if (isUnitPropertiesToggleActive) {
boolean clickUnitPropertiesIcon = loggedInPage.clickUnitPropertiesIcon();
Common.reportAssert(driver, thisClass, " click Unit Properties Icon?", clickUnitPropertiesIcon);
// System.exit(0);
}
}
我可以通过将第一行更改为:
来强制它在测试后运行@AfterClass
public void tearDown() {
但是,下一个类(下面)与第一个类具有完全相同的符号,但它设法仅在末尾运行tearDown()(正确)
@Test(groups = { "all", "defects" })
public void tearDown() {
Common.myPrint(thisClass + " CreateDefectExistingCallerTest tearDown invoked.");
boolean isDefectDescriptionDisplayed=loggedInPage.isDefectDescriptionDisplayed();
if(isDefectDescriptionDisplayed) {
Common.reportAssert(driver, thisClass, " is Defect Description Displayed?", isDefectDescriptionDisplayed);
boolean clickBackArrow = loggedInPage.goBack();
Common.reportAssert(driver, thisClass, " click Back Arrow?", clickBackArrow);
Common.isloadComplete(driver);
Common.pause(5000);
}
boolean isDefectsAssetsDisplayed = loggedInPage.isDefectAssetsDisplayed();
if (isDefectsAssetsDisplayed) {
Common.reportAssert(driver, thisClass, " is Defects Assets Displayed?", isDefectsAssetsDisplayed);
boolean clickBackArrow = loggedInPage.goBack();
Common.reportAssert(driver, thisClass, " click Back Arrow?", clickBackArrow);
Common.isloadComplete(driver);
Common.pause(5000);
}
boolean isActivitiesDisplayed = loggedInPage.isactivitiesDisplayed();
if (isActivitiesDisplayed) {
boolean clickBackArrow = loggedInPage.goBack();
Common.reportAssert(driver, thisClass, " click Back Arrow?", clickBackArrow);
Common.isloadComplete(driver);
Common.pause(5000);
}
boolean isHomeDisplayed=loggedInPage.isHomeDisplayed();
if(isHomeDisplayed) {
boolean clickBackArrow = loggedInPage.goBack();
Common.reportAssert(driver, thisClass, " click Back Arrow?", clickBackArrow);
}
}
为什么这有时会按预期工作,&其他时间不是吗?
无论我是使用testng.xml将其作为套件的一部分运行,还是使用Eclipse>自行运行,都会发生这种情况。作为TestNG测试运行。
这是testng.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="tests">
<parameter name="browser" value="firefox" />
<parameter name="printToFile" value="false" />
<listeners>
<listener class-name="listeners.TestNGCustomReportListener" />
</listeners>
<test thread-count="5" name="firefoxTest">
<groups>
<define name="all">
<include name="all" />
</define>
<run>
<include name="all" />
<exclude name="not-implemented" />
</run>
</groups>
<classes>
<class name="tests.login.LoginTest" />
<class name="tests.login.AutoLogOutTest" />
<class name="tests.login.AutoLogOutWithRestApiTest" />
<class name="tests.login.ChangePasswordTest" />
<class name="tests.login.SwitchCustomerProjectsTest" />
<class name="tests.login.ForgotPasswordResetTest" />
<class name="tests.login.EditUserNameTest" />
<class name="tests.layers.EnableLayerTest" />
<class name="tests.layers.EnableLayerStyleTest" />
<class name="tests.layers.ChangeLayerNameTest" />
<class name="tests.layers.ChangeStyleNameTest" />
<class name="tests.layers.ChangeStyleColourTest" />
<!-- unimplemented tests -->
<class name="tests.layers.EditStyleIconTest" />
<class name="tests.layers.CloneStandardLayerTest" />
<class name="tests.layers.ViewCustomStyleTest" />
<class name="tests.layers.CreateCustomLayerTest" />
<class name="tests.layers.AddStyleCustomLayerTest" />
<class name="tests.layers.EditStyleBuildRuleTest" />
<class name="tests.layers.EditStyleNameCustomTest" />
<class name="tests.layers.EditStyleColourCustomTest" />
<class name="tests.layers.EditStyleIconCustomTest" />
<class name="tests.layers.BuildCustomLayerTest" />
<class name="tests.layers.DeleteCustomLayerTest" />
<class name="tests.layers.CloneCustomLayerTest" />
<class name="tests.layers.QueryStartsWithTest" />
<class name="tests.layers.QueryEndsWithTest" />
<class name="tests.layers.QueryContainsTest" />
<class name="tests.layers.QueryGreaterThanTest" />
<class name="tests.layers.QueryLessThanTest" />
<class name="tests.layers.QueryAddTest" />
<class name="tests.layers.QuerySubtractTest" />
<class name="tests.layers.QueryDivideTest" />
<class name="tests.layers.QueryMultiplyTest" />
<class name="tests.layers.QueryPowerTest" />
<class name="tests.layers.QueryRootTest" />
<class name="tests.layers.QueryLowerTest" />
<class name="tests.layers.QueryUpperTest" />
<class name="tests.layers.QueryLengthTest" />
<class name="tests.layers.QueryRoundTest" />
<class name="tests.layers.QueryCeilingTest" />
<class name="tests.layers.QueryFloorTest" />
<class name="tests.networks.SwitchNetworksTest" />
<class name="tests.networks.ViewNetworkTest" />
<class name="tests.networks.EditNetworkStyleColourTest" />
<class name="tests.networks.EditNetworkStyleIconTest" />
<class name="tests.networks.EditNetworkStyleNameTest" />
<class name="tests.networks.EditNetworkNameTest" />
<class name="tests.networks.DeleteNetworkLayerTest" />
<class name="tests.networks.EditNetworkSectionTest" />
<class name="tests.networks.EditNetworkGeometryTest" />
<class name="tests.networks.ViewNetworkHistoryTest" />
<class name="tests.basemaps.SwitchBasemapsTest" />
<class name="tests.basemaps.AddWmsLayerTest" />
<class name="tests.basemaps.DeleteBasemapTest" />
<class name="tests.jobs.ViewJobsLayerTest" />
<class name="tests.jobs.CreateNewJobTest" />
<class name="tests.jobs.CreateGroupJobTest" />
<class name="tests.jobs.CreateDefectJobTest" />
<class name="tests.jobs.ViewYourJobsTest" />
<class name="tests.jobs.ViewAllJobsTest" />
<class name="tests.jobs.DeleteJobTest" />
<class name="tests.jobs.ViewJobDetailsTest" />
<class name="tests.jobs.EditJobDetailsTest" />
<class name="tests.jobs.EditJobLayerStyleTest" />
<class name="tests.jobs.AllJobsOnAssetTest" />
<class name="tests.jobs.ViewJobReportTest" />
<class name="tests.jobs.OpenJobsReportTest" />
<class name="tests.jobs.ClosedJobsReportTest" />
<class name="tests.jobs.MarkJobCompleteTest" />
<class name="tests.jobs.ShowHideJobGraphTest" />
<class name="tests.jobs.ViewJobHistoryTest" />
<class name="tests.jobs.EditJobWorkItemTest" />
<class name="tests.jobs.DeleteJobWorkItemTest" />
<class name="tests.inspections.ViewInspectionLayerTest" />
<class name="tests.inspections.CreateInspectionTest" />
<class name="tests.inspections.CreateInspectionResourceTemplaterTest" />
<class name="tests.inspections.CreateInspectionGroupTest" />
<class name="tests.inspections.CreateInspectionDefectTest" />
<class name="tests.inspections.ViewInspectionsTest" />
<class name="tests.inspections.ViewAllInspectionsTest" />
<class name="tests.inspections.DeleteInspectionTest" />
<class name="tests.inspections.InspectionDetailsTest" />
<class name="tests.inspections.EditInspectionTest" />
<class name="tests.inspections.EditInspectionLayerStyleTest" />
<class name="tests.inspections.ViewAssetInspectionsTest" />
<class name="tests.inspections.ViewInspectionReportTest" />
<class name="tests.inspections.OpenInspectionsReportTest" />
<class name="tests.inspections.CompletedInspectionsReportTest" />
<class name="tests.inspections.CompleteInspectionTest" />
<class name="tests.inspections.InspectionGraphTest" />
<class name="tests.inspections.InspectionHistoryTest" />
<class name="tests.defects.ViewDefectsLayerTest" />
<class name="tests.defects.CreateDefectTest" />
<class name="tests.defects.CreateDefectNewCallerTest" />
<class name="tests.defects.CreateDefectExistingCallerTest" />
<class name="tests.defects.CreateInspectionOnDefectTest" />
<class name="tests.defects.ViewAllDefectsTest" />
<class name="tests.defects.DeleteDefectTest" />
<class name="tests.defects.ViewDefectDetailsTest" />
<class name="tests.defects.EditDefectDetailsTest" />
<class name="tests.defects.ViewAssetDefectsTest" />
<class name="tests.defects.ViewDefectReportTest" />
<class name="tests.defects.OpenDefectsReportTest" />
<class name="tests.defects.CompletedDefectsReportTest" />
<class name="tests.defects.CompleteDefectInspectionTest" />
<class name="tests.defects.ViewDefectHistoryTest" />
<class name="tests.projects.ViewProjectsTest" />
<class name="tests.projects.CreateProjectTest" />
<class name="tests.projects.AddProjectTaskTest" />
<class name="tests.projects.RemoveProjectTaskTest" />
<class name="tests.projects.AddJobExistingProjectTest" />
<class name="tests.projects.AddJobNewProjectTest" />
<class name="tests.projects.AddInspectionExistingProjectTest" />
<class name="tests.projects.AddInspectionNewProjectTest" />
<class name="tests.projects.AddTasksGroupTest" />
<class name="tests.projects.RemoveProjectTasksTest" />
<class name="tests.projects.ViewProjectDetailsTest" />
<class name="tests.projects.ProjectGraphTest" />
<class name="tests.projects.ViewAllProjectTasksTest" />
<class name="tests.projects.EditProjectTest" />
<class name="tests.projects.DeleteProjectTest" />
<class name="tests.projects.ViewProjectProgressTest" />
<class name="tests.projects.ViewProjectReportTest" />
<class name="tests.projects.ViewProjectTeamsTest" />
<class name="tests.projects.CloseProjectTest" />
<class name="tests.projects.ViewProjectAssetsTest" />
<class name="tests.projects.ChangeTaskProjectTest" />
<class name="tests.teams.ViewAllTeamsTest" />
<class name="tests.teams.CreateTeamTest" />
<class name="tests.teams.AddTeamMemberTest" />
<class name="tests.teams.RemoveTeamMemberTest" />
<class name="tests.teams.AddMemberExistingTeamTest" />
<class name="tests.teams.AddMemberNewTeamTest" />
<class name="tests.teams.AddGroupExistingTeamTest" />
<class name="tests.teams.RemoveAllTeamMembersTest" />
<class name="tests.teams.ViewTeamDetailsTest" />
<class name="tests.teams.TeamGraphTest" />
<class name="tests.teams.ViewTeamMembersTest" />
<class name="tests.teams.EditTeamDetailsTest" />
<class name="tests.teams.DeleteTeamTest" />
<class name="tests.teams.TeamReportTest" />
<class name="tests.teams.ViewTeamProjectsTest" />
<class name="tests.teams.ViewTeamHistoryTest" />
<class name="tests.teams.TeamSearchTest" />
<class name="tests.teams.PromoteTeamMembersTest" />
<class name="tests.teams.DemoteTeamMembersTest" />
<class name="tests.routes.ViewRoutesTest" />
<class name="tests.routes.ViewRouteTest" />
<class name="tests.routes.ViewRouteDetailsTest" />
<class name="tests.routes.EditRouteNameTest" />
<class name="tests.routes.EditRouteColourTest" />
<class name="tests.routes.ViewRouteOnMapTest" />
<class name="tests.routes.ViewRouteProjectTest" />
<class name="tests.routes.ViewRouteHistoryTest" />
<class name="tests.routes.ViewHistoryEntryTest" />
<class name="tests.routes.EditHistoryItemTest" />
<class name="tests.routes.ViewRouteReportTest" />
<class name="tests.routes.ViewRouteWaypointsTest" />
<class name="tests.routes.AddStartPoint1Test" />
<class name="tests.routes.AddStartPoint2Test" />
<class name="tests.routes.RemoveStartPointTest" />
<class name="tests.routes.AddEndPoint1Test" />
<class name="tests.routes.AddEndPoint2Test" />
<class name="tests.routes.RemoveEndpointTest" />
<class name="tests.routes.AddWayPointTest" />
<class name="tests.routes.RemoveWayPointTest" />
<class name="tests.routes.ChangeWayPointOrderTest" />
<class name="tests.routes.OptimizeRouteTest" />
<class name="tests.routes.CreateRouteTest" />
<class name="tests.routes.CreateRouteAddStartTest" />
<class name="tests.routes.CreateRouteRemoveStartTest" />
<class name="tests.routes.CreateRouteRemoveTaskTest" />
<class name="tests.routes.CreateRouteAddTaskTest" />
<class name="tests.search.SearchByItemIdTest" />
<class name="tests.search.SearchAttributeValueTest" />
<class name="tests.search.SearchCollectionsTest" />
<class name="tests.search.SearchDesignsTest" />
<class name="tests.search.SearchFilesTest" />
<class name="tests.search.SearchFoldersTest" />
<class name="tests.search.SearchGroupsTest" />
<class name="tests.search.SearchIdsTest" />
<class name="tests.search.SearchImportsTest" />
<class name="tests.search.SearchInspectionsTest" />
<class name="tests.search.SearchLocationsTest" />
<class name="tests.search.SearchReportsTest" />
<class name="tests.search.SearchSchedulesTest" />
<class name="tests.search.SearchUsersTest" />
<class name="tests.search.SearchWorkflowsTest" />
<class name="tests.search.SearchRoutesTest" />
<class name="tests.search.SearchProjectsTest" />
<class name="tests.search.SearchTeamsTest" />
<class name="tests.search.SearchDesignNameTest" />
<class name="tests.schedules.ViewAllSchedulesTest" />
<class name="tests.schedules.ViewCalendarScheduleDetailsTest" />
<class name="tests.schedules.ViewCalendarScheduleDatesTest" />
<class name="tests.schedules.CyclicScheduleDetailsTest" />
<class name="tests.schedules.CyclicScheduleDatesTest" />
<class name="tests.schedules.EditScheduleDetailsTest" />
<class name="tests.schedules.EditCyclicScheduleDatesTest" />
<class name="tests.schedules.DeleteScheduleTest" />
<class name="tests.schedules.ViewScheduleHistoryTest" />
<class name="tests.schedules.ViewScheduleReportTest" />
<class name="tests.schedules.JobCalendarScheduleTest" />
<class name="tests.schedules.InspectionCalendarScheduleTest" />
<class name="tests.schedules.ProjectCalendarScheduleTest" />
<class name="tests.schedules.JobCyclicScheduleTest" />
<class name="tests.schedules.InspectionCyclicScheduleTest" />
<class name="tests.schedules.ProjectCyclicScheduleTest" />
<class name="tests.gateway.ViewAllFilesTest" />
<class name="tests.gateway.ViewAllFoldersTest" />
<class name="tests.gateway.ViewAllImportsTest" />
<class name="tests.gateway.UploadFileTest" />
<class name="tests.gateway.DragAndDropFileTest" />
<class name="tests.gateway.ImportCentrelineNetworkTest" />
<class name="tests.gateway.ImportNsgNetworkTest" />
<class name="tests.gateway.ImportGeometryNetworkTest" />
<class name="tests.gateway.ViewFileDetailsTest" />
<class name="tests.gateway.DeleteFileTest" />
<class name="tests.gateway.ViewFolderDetailsTest" />
<class name="tests.gateway.DeleteFolderTest" />
<class name="tests.gateway.ViewImportDetailsTest" />
<class name="tests.gateway.DeleteImportTest" />
<class name="tests.permissions.ViewAllUsersTest" />
<class name="tests.permissions.ViewAllUserGroupsTest" />
<class name="tests.permissions.ViewUserDetailsTest" />
<class name="tests.permissions.ViewUsersGroupsTest" />
<class name="tests.permissions.ViewUsersModulesTest" />
<class name="tests.permissions.ViewAccessAdvisorTest" />
<class name="tests.permissions.EditUserDetailsTest" />
<class name="tests.permissions.ArchiveUserTest" />
<class name="tests.permissions.ViewUserHistoryTest" />
<class name="tests.permissions.ViewUserReportTest" />
<class name="tests.permissions.AddUserToGroupTest" />
<class name="tests.permissions.RemoveUserFromGroupsTest" />
<class name="tests.permissions.AddModuleToUserTest" />
<class name="tests.permissions.RemoveModulesFromUserTest" />
<class name="tests.permissions.ViewGroupDetailsTest" />
<class name="tests.permissions.ViewUsersInGroupTest" />
<class name="tests.permissions.ViewGroupPermissionsTest" />
<class name="tests.permissions.EditGroupDetailsTest" />
<class name="tests.permissions.EditUsersInGroupTest" />
<class name="tests.permissions.RemoveUsersFromGroupTest" />
<class name="tests.permissions.ArchiveGroupTest" />
<class name="tests.permissions.DeleteGroupTest" />
<class name="tests.permissions.EditGroupPermissionsTest" />
<class name="tests.permissions.AddAttributesReadTest" />
<class name="tests.permissions.AddAttributesWriteTest" />
<class name="tests.permissions.RemoveAttributesTest" />
<class name="tests.permissions.MakeDesignsReadTest" />
<class name="tests.permissions.MakeDesignsWriteTest" />
<class name="tests.permissions.ViewGroupReportTest" />
<class name="tests.permissions.ChangeTeamUserTest" />
<class name="tests.mesh.ViewLightSensorTest" />
<class name="tests.mesh.ViewLampSensorDetailsTest" />
<class name="tests.mesh.TurnOnLampTest" />
<class name="tests.mesh.TurnOffLampTest" />
<class name="tests.designer.ViewAllDesignsTest" />
<class name="tests.designer.ViewDesignDetailTest" />
<class name="tests.designer.ViewAssetDesignGraphTest" />
<class name="tests.designer.ViewDesignAttributesTest" />
<class name="tests.designer.EditDesignNameTest" />
<class name="tests.designer.EditDesignIconTest" />
<class name="tests.designer.EditDesignColourTest" />
<class name="tests.designer.EditDesignAttributesTest" />
<class name="tests.designer.EditDesignPropertiesTest" />
<class name="tests.designer.RemoveDesignAttributesTest" />
<class name="tests.designer.ViewDesignInspectionQuestionsTest" />
<class name="tests.designer.EditDesignInspectionQuestionsTest" />
<class name="tests.designer.ViewInspectionDesignGraphTest" />
<class name="tests.designer.RemoveInspectionQuestionsTest" />
<class name="tests.designer.EditInspectionAssetsTest" />
<class name="tests.designer.CreateAssetDesignTest" />
<class name="tests.designer.CreateNetworkDesignTest" />
<class name="tests.designer.CreateInspectionDesignTest" />
<class name="tests.designer.CreateDefectDesignTest" />
<class name="tests.designer.CreateSystemDataDesignTest" />
<class name="tests.designer.AttemptDeleteDesignTest" />
<class name="tests.designer.DeleteInspectionDesignTest" />
<class name="tests.designer.DeleteDesignTest" />
<class name="tests.designer.AddAttributesNewAssetTest" />
<class name="tests.designer.CreateItemNewAssetTest" />
<class name="tests.workflow.ViewAllWorkflowsTest" />
<class name="tests.workflow.ViewWorkflowDetailsTest" />
<class name="tests.workflow.EditWorkflowDetailsTest" />
<class name="tests.workflow.AddWorkflowCheckTest" />
<class name="tests.workflow.DeleteWorkflowCheckTest" />
<class name="tests.workflow.DeleteAllWorkflowsCheckTest" />
<class name="tests.workflow.WorkflowAddEmailTest" />
<class name="tests.workflow.WorkflowEditResourceTest" />
<class name="tests.workflow.WorkflowDeleteResourceTest" />
<class name="tests.workflow.WorkflowCallUrlTest" />
<class name="tests.workflow.WorkflowSendEmailTest" />
<class name="tests.workflow.TriggerEditResourceTest" />
<class name="tests.workflow.TriggerDeleteResourceTest" />
<class name="tests.workflow.TriggerSendUrlCallTest" />
<class name="tests.workflow.DeleteWorkflowActionTest" />
<class name="tests.workflow.DeleteAllWorkflowActionsTest" />
<class name="tests.workflow.CreateNewWorkflowTest" />
<class name="tests.workflow.DisableWorkflowTest" />
<class name="tests.workflow.EnableWorkflowTest" />
<class name="tests.workflow.DeleteWorkflowTest" />
<class name="tests.workflow.CreateCheckWithErrorsTest" />
<class name="tests.workflow.EditCheckTest" />
<class name="tests.workflow.ManageWorkflowAttributesTest" />
<class name="tests.reports.ViewAllReportsTest" />
<class name="tests.reports.ViewReportsDetailsTest" />
<class name="tests.reports.ViewPreviousReportsTest" />
<class name="tests.reports.GenerateNewReportTest" />
<class name="tests.reports.DeleteReportTest" />
<class name="tests.streetlighting.ViewStreetlightsLayerTest" />
<class name="tests.streetlighting.ViewStreetlightsNightscapeTest" />
<class name="tests.streetlighting.PlaceNewUnitWithMapTest" />
<class name="tests.streetlighting.PlaceNewUnitManuallyTest" />
<class name="tests.streetlighting.CreateUnitWithoutIdTest" />
<class name="tests.streetlighting.CreateUnitWithoutCoordsTest" />
<class name="tests.streetlighting.CancelCreatingUnitTest" />
<class name="tests.streetlighting.ViewStreetlightsDetailsTest" />
<class name="tests.streetlighting.EditStreetlightsDetailsTest" />
<class name="tests.streetlighting.AddStreetlightComponentTest" />
<class name="tests.streetlighting.RemoveStreetlightComponentTest" />
<class name="tests.streetlighting.ViewStreetlightComponentDetailsTest" />
<class name="tests.streetlighting.EditStreetlightGeometryTest" />
<class name="tests.streetlighting.ViewStreetlightComponentGraphTest" />
<class name="tests.collections.ViewAllCollectionsTest" />
<class name="tests.collections.ViewCollectionDetailsTest" />
<class name="tests.collections.ViewCollectionItemsTest" />
<class name="tests.collections.ViewCollectionHistoryTest" />
<class name="tests.collections.AddItemToCollectionTest" />
<class name="tests.collections.AddGeometryItemToCollectionTest" />
<class name="tests.collections.ViewCollectionItemDetailsTest" />
<class name="tests.collections.EditCollectionItemDetailsTest" />
<class name="tests.collections.EditCollectionItemGeometryTest" />
<class name="tests.collections.DeleteCollectionItemTest" />
<class name="tests.collections.ViewCollectionReportTest" />
<class name="tests.collections.ViewItemReportTest" />
<class name="tests.collections.CreateNewCollectionTest" />
<class name="tests.collections.DeleteCollectionTest" />
</classes>
</test>
</suite>
最终编辑:
我决定使用@After类方法运行,因为它更加一致:
@AfterClass
public void tearDown() {
答案 0 :(得分:0)
如果你真的想用testng执行junit测试,请在doc中注意属性的变化:
TestNG可以运行JUnit 3和JUnit 4测试。您需要做的就是将JUnit jar文件放在类路径上,在testng.classNames属性中指定您的JUnit测试类,并将testng.junit属性设置为true:
<test name="Test1" junit="true">
然后注意JUnit的tearDown method应该被覆盖如下:
@Override
protected void tearDown()
throws java.lang.Exception