单元自动化测试:基于JUnit设计自动化测试用例
程序员文章站
2024-03-15 18:58:48
...
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lujiatao</groupId>
<artifactId>unittest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Unit Test</name>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package com.lujiatao.unittest;
public class CalculatorForPpi {
public static double calculate(int width, int height, double size) {
long result;
if (width > 0 && height > 0 && size > 0) {
result = Math.round(Math.pow((Math.pow(width, 2) + Math.pow(height, 2)) / Math.pow(size, 2), 0.5));
} else {
result = -1;
}
return result;
}
}
package com.lujiatao.unittest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
public class CalculatorForPpiTest {
private static int width;
private static int height;
private static double size;
@BeforeAll
static void init() {
width = 750;
height = 1334;
size = 4.7;
}
@Test
void testCase1() {
Assertions.assertEquals(326, CalculatorForPpi.calculate(width, height, size));
}
@Test
void testCase2() {
Assertions.assertEquals(-1, CalculatorForPpi.calculate(-1, height, size));
}
@Test
void testCase3() {
Assertions.assertEquals(-1, CalculatorForPpi.calculate(0, height, size));
}
@Test
void testCase4() {
Assertions.assertEquals(-1, CalculatorForPpi.calculate(width, -1, size));
}
@Test
void testCase5() {
Assertions.assertEquals(-1, CalculatorForPpi.calculate(width, 0, size));
}
@Test
void testCase6() {
Assertions.assertEquals(-1, CalculatorForPpi.calculate(width, height, -1));
}
@Test
void testCase7() {
Assertions.assertEquals(-1, CalculatorForPpi.calculate(width, height, 0));
}
}
上一篇: 求0到10的阶乘之和