单元自动化测试:基于JUnit设计自动化测试用例

 

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值