测试期间Java重复

问题描述 投票:1回答:1

我在Ibello和Java中进行过测试。我用

int random = (int)(Math.random() * (100000 - 1) + 1);

生成5位数代码。

我有一个测试类:

pthUgyintezesNewCase.I_Use_the_5digits();

allatiMellektermekSteps.Delete_the_old_datas();

pthNewCase.I_Save_And_Close(); //it brokes here

// Steps Class

public void I_Use_the_5digits() throws Exception{
        int random = (int)(Math.random() * (100000 - 1) + 1);

//

如果应用程序生成比以往更多的5位数字,它就不会运行。是否有可能,如果它再次产生似乎,它会在一个数字上加1,直到它可以运行?

java selenium testing random cucumber
1个回答
0
投票

我想你想要生成一个5位数字,但有时候这个数字只有4位数?如果是这种情况,请尝试以下操作:

(int)(Math.random() * (100000 - 10000) + 10000);

通过使用10000而不是1,您可以确保该数字始终包含至少5位数

编辑:

这是一个检查下一个10'000'000数字的测试用例:

import java.lang.Math; 
import java.util.Random;

public class HelloWorld
{
  public static void main(String[] args)
  {
    for(int i = 0; i < 10000000; i++) {
        int number = (int)(Math.random() * (100000 - 10000) + 10000);
        if(String.valueOf(number).length() != 5) {
            System.out.println(String.valueOf(number).length());
            System.exit(0);
        }
    }
    System.out.println("Finished with all 5 digit numbers");
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.