gpt4 book ai didi

java - 在基于环境变量的 Before/Setup 方法中跳过/忽略 JUnit 测试用例

转载 作者:行者123 更新时间:2023-11-29 04:32:15 30 4
gpt4 key购买 nike

我有很多类是从具有多个 @Test 案例的抽象 JUnit 类扩展而来的。问题是,在某些此类扩展类中,仅当一个环境变量具有特定值时,我才需要跳过/忽略所有测试用例。

例如,在 ruby​​ 测试套件中,我可以在 before 方法中执行以下操作:

require 'rubygems'
require 'minitest/spec'
require 'minitest/autorun'
require 'selenium-webdriver'

class WebPcAr < MiniTest::Test

def setup

...

### SET URLS
case ENV['ENVIRONMENT_NAME']
when "test"
skip
when "preprod"
skip
when "prod"
@base_url = 'http://www.google.com.ar/'
else
skip
end
end

有没有办法在 Java 中做同样的事情?我所能找到的只是@Ignore 方法,但是由于类运行的@Tests 不在同一个类中,而是在抽象类中,并且那些@Tests 在其他几个类之间共享,我无法添加@忽略那些 @Test 用例的标记。也许使用仅存在于那些需要它的特定扩展类中的@Rule?

请记住,@Tests 不在我的类中,而是与其他不需要跳过/忽略它们的类共享的抽象类。而且我需要在同一次运行中运行所有类,我不能将特定于类的参数传递给 Maven 命令,因为它需要特定于类。

我在这里找到的其他方式Conditionally ignoring tests in JUnit 4是使用行“org.junit.Assume.assumeTrue(someCondition());”但我不明白如何将它与我的环境变量一起使用:

 @Before
public void beforeMethod() {
org.junit.Assume.assumeTrue(someCondition());
// rest of setup.
}

也许类似于 Ignore Test Cases Based on Environment ?那不是跳过/忽略测试而是给我一个“org.junit.AssumptionViolatedException:测试”错误

@Override
public void setUp() throws Exception {
assumeTrue(System.getenv("AMBIENTE"), equals("prod"));
super.setUp();
...

编辑:这是我的测试类:

package My.Site.suitesWeb;

import static org.junit.Assert.assertTrue;
//import static org.junit.Assume.assumeTrue;

import org.junit.Ignore;
import org.junit.Test;

import My.Site.classesWeb.*;

public class TestWebPcMySite extends WebPcBase {

@Override
public void setUp() throws Exception {
// Runs only in prod ENVIRONMENT
String currentEnv = System.getenv("AMBIENTE");
String expectedEnv = "prod";
assumeTrue(currentEnv == expectedEnv );
// Run parent class setup
super.setUp();
// Override with specific classes
goTo = new ObjGoToPageWebPcMySite(driver);
homePage = new ObjHomePageWebPcMySite(driver);
loginPage = new ObjLoginPageWebPcMySite(driver);
}

// I IGNORE SOME METHODS FROM EXTENDED CLASS
@Ignore
@Test
public void testHigh_LandingPage() throws Throwable {
}

@Ignore
@Test
public void testLow_LandingPage_LinkLogin() throws Throwable {
}

@Ignore
@Test
public void testLow_LandingPage_LinkRegister() throws Throwable {
}

// I OVERRIDE OTHER METHODS WITH OTHER CODE
@Test
public void testHigh_UserLogin_OkUserWithSuscriptionActive() throws Throwable {
try {
// Test data

// Test case
goTo.homePage(baseUrl);
homePage.loginButton().click();

loginPage.userLogin(usernameLogin, passwordLogin);
assertTrue(homePage.profileNameButton().isDisplayed());
} catch (Throwable e) {
utilsCommon.errorHandle(e, suiteName, testName);
throw e;
}
}

}

最佳答案

在您的第一个示例中,您只需将“someCondition()”替换为您的环境检查,例如:

@Before
public void beforeMethod() {
String currentEnv = someMethodToGetYourEnvironment();
String expectedEnv = "DEV";
org.junit.Assume.assumeTrue(currentEnv == expectedEnv );
// rest of setup.
}

您可能需要注意 @Before 在每个方法之前运行。由于这是一个环境检查,因此在类级别检查它可能更有意义。相同的概念,但使用 @BeforeClass 代替。

关于java - 在基于环境变量的 Before/Setup 方法中跳过/忽略 JUnit 测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43375834/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com