testNG优先事项&依赖

时间:2016-05-02 11:57:14

标签: java testng

我对优先事项有一些问题&对testNG的依赖。例如,如果我有这个

@Test
public void login () { ... }

@Test (dependsOnMethods = {"login"})
public void method1 () { ... }

@Test (dependsOnMethods = {"method1"})
public void method2 () { ... }

@Test (dependsOnMethods = {"login"})
public void logout () { ... }

在这种情况下,它运行如下:

登录 - > method1 - >注销 - >方法2

因为我已经不再联系了,所以不会工作

你要对我说,退出取决于方法2,一切都会有效......

是的它会...但是当method1或2失败时,它会跳过注销而不是这样做......而这不是我想要的。

然后你会说我......在这种情况下,只用优先级而不是依赖关系运行它很容易....但是如果方法1失败了那么...那么方法2可能是好的但是赢了'因为method1失败所以我会有误报。

关于如何做到这一点的任何想法?

5 个答案:

答案 0 :(得分:0)

您应该使用之前/之后的方法来执行此操作:

@BeforeClass
public void login () { ... }

@Test
public void method1 () { ... }

@Test(dependsOnMethods = {"method1"})
public void method2 () { ... }

@AfterClass(alwaysRun=true) // alwaysRun to run the after method even if a test fails
public void logout () { ... }

答案 1 :(得分:0)

您可以声明logout依赖于“method2”和always run

  

alwaysRun

     

public abstract boolean alwaysRun

     

如果设置为true,则即使依赖于失败的方法,也始终会运行此测试方法。如果此测试不依赖于任何方法或组,则将忽略此属性。

     

默认:

     

false

示例:

@Test
public void login () { ... }

@Test (dependsOnMethods = {"login"})
public void method1 () { ... }

@Test (dependsOnMethods = {"method1"})
public void method2 () { ... }

@Test (dependsOnMethods = {"method2"}, alwaysRun = true)
public void logout () { ... }

答案 2 :(得分:0)

您可以将方法定义为:

        @Test(priority=1)
public void login()
{
    System.out.println("login");
}

@Test(priority=2)
public void method1()
{
    System.out.println("Method1");
    Assert.assertEquals("Method1", "login", "invalid msg not verified"); // Here test is failing as the expected value is not matching the actual value
    System.out.println("Verified");
}

@Test(priority=3)
public void method2()
{
    System.out.println("Method2");
    Assert.assertEquals("Method2", "Method2", "invalid msg not verified"); // Method1 fails however method2 is executed as actual and expected value are matching.
    System.out.println("Verified");
}

@Test(priority=4)
public void logout()
{
    System.out.println("logout");
}

这将帮助您执行所有优先级为1的测试,然后只有优先级为2,然后是3,然后是4.我已添加" Assert"以上代码将帮助您在任何测试失败的情况下进一步发展。

答案 3 :(得分:0)

我相信当问题很好地集思广益的同时,在实施过程中很少有概念被忽略。希望这能解决问题:

@Test
public void login () { ... }

@Test (dependsOnMethods = {"login"}, groups = { "afterLogin" })
public void method1 () { ... }

@Test (dependsOnMethods = {"method1"}, groups = { "afterLogin" }, priority = 1) 
public void method2 () { ... }
// you mark dependsOnMethods when it actually "depends on" something prior to its execution and 
// hence corresponds to the failure of method it is depending on

@Test (dependsOnMethods = {"login"}, priority = 2)
public void logout () { ... }

这将确保序列为 登录 - > method1 - > method2 - >注销

其中,

案例1 :如果login失败,则依赖于的方法不应执行。

案例2 :如果方法1或2失败,则不会跳过logout测试

案例3 :如果方法1和方法2不依赖于登录,您实际上可以优先级更低/更高/相等地执行它们,确保they are all independent

答案 4 :(得分:0)

可能对您有帮助

let a = { "authenticationResultCode": "ValidCredentials", "brandLogoUri": "http://dev.virtualearth.net/Branding/logo_powered_by.png", "copyright": "Copyright © 2019 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.", "resourceSets": [ { "estimatedTotal": 1, "resources": [ { "__type": "Location:http://schemas.microsoft.com/search/local/ws/rest/v1", "bbox": [ 40.75594078242932, -74.0022632570927, 40.76366621757067, -73.98866508290732 ], "name": "471 W 42nd St, New York, NY 10036", "point": { "type": "Point", "coordinates": [ 40.7598035, -73.99546417 ] }, "address": { "addressLine": "471 W 42nd St", "adminDistrict": "NY", "adminDistrict2": "New York Co.", "countryRegion": "United States", "formattedAddress": "471 W 42nd St, New York, NY 10036", "intersection": { "baseStreet": "W 42nd St", "secondaryStreet1": "10th Ave", "intersectionType": "Near", "displayName": "W 42nd St and 10th Ave" }, "locality": "New York", "postalCode": "10036" }, "confidence": "High", "entityType": "Address", "geocodePoints": [ { "type": "Point", "coordinates": [ 40.7598035, -73.99546417 ], "calculationMethod": "Rooftop", "usageTypes": [ "Display" ] } ], "matchCodes": [ "Good" ] } ] } ], "statusCode": 200, "statusDescription": "OK", "traceId": "d551494203554f058cd3e2e72582f7b1|HK20271557|7.7.0.0|HK01EAP000001D0" }