简体   繁体   English

JUnit 前测试参数

[英]JUnit before test parameters

Is there a way to give test somesort of parameters that need to be used before the test is run.有没有办法为测试提供在运行测试之前需要使用的某种参数。 Like log in with different account or something like that?像使用不同的帐户登录或类似的东西?

I want to test a webpage.我想测试一个网页。 The webpage has different behaviour depending on the user who is logged in. Before each test i would like to switch the user who is logged in. Is there a way to let @Before know what user i want to log in with?网页有不同的行为取决于登录的用户。在每次测试之前,我想切换登录的用户。有没有办法让@Before 知道我想用哪个用户登录?

You can write helper methods which will do your login.您可以编写帮助方法来完成您的登录。 You can use these methods in your tests, depending on what you want to test/check.您可以在测试中使用这些方法,具体取决于您要测试/检查的内容。

@Test
public void deleteUserComment() {
    int adminId = this.loginAsAdministrator();
    /*
    ...
    */
}

@Test
public void saveSettings() {
    int userId = this.loginAsUser();
    /*
    ...
    */
}
int x = 0;

@BeforeEach
public void beforeEach() {
    switch(x) {
        case 0:
            //Log in with user 1
            break;
        case 1:
            //Log in with user 2
            break;
    } 
    x++:
}

Should work.应该管用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM