table of Contents
1. selection and installation of development tools
Since we have not yet finally decided to follow-up development projects which use specific programming language, so the decision to install Microsoft Visual Studio 2017 Community Edition and Eclipse IDE two development tools, one for C ++ / C # and Java development.
1.1 Visual Studio 2017
Visual Studio is the most I write C, C ++, C #, Python main use development tools, so my computer has been the development tools installed.
It runs in a state as shown in FIG.
Eclipse 1.2 IDE
Eclipse installation is more complicated, divided into the following steps
- Install Java SE Development Kit
- Install Java SE Runtime Environment
- Configuring the system environment variables
- Editing system environment variable Path
- New system environment variables CLASSPATH
- Editing system environment variable Path
- Check the Java environment installation and configuration.
- Install Eclipse IDE
- Start Eclipse IDE
2. Automatic Test Unit
As the last step to configure the two development environments, so we were C # and Java unit testing in both development environments.
2.1 C # unit test
In Visual Studio 2017 version, the original no longer exists VSTS tools. There are three test methods can be used.
- Microsoft Azure DevOps provide testing tools
- Visual Studio 2017 comes with testing tools MSTest
- Visual Studio NUnit test plug-in 2017
我们在这里选择Visual Studio自带的测试工具MSTest进行测试。
首先编写用于测试的类库User
namespace UnitTest2
{
public class User
{
public User(string userEmail)
{
m_email = userEmail;
}
private string m_email; //user email as user id
}
}
然后创建单元测试
得到下面的自动生成但是还不能直接运行的测试类
namespace UnitTest2.Tests
{
[TestClass()]
public class UserTests
{
[TestMethod()]
public void UserTest()
{
Assert.Fail();
}
}
}
修改后得到最终的测试类
namespace UnitTest2.Tests
{
[TestClass()]
public class UserTests
{
[TestMethod()]
public void UserTest()
{
string userEmail = "[email protected]";
User target = new User(userEmail);
Assert.IsTrue(target != null);
}
}
}
运行测试后得到如图的结果
添加更多的测试,将测试类更改为
namespace UnitTest2.Tests
{
[TestClass()]
public class UserTests
{
[TestMethod()]
public void UserTest()
{
string userEmail = "[email protected]";
User target = new User(userEmail);
Assert.IsTrue(target != null);
}
[TestMethod()]
[ExpectedException(typeof(ArgumentException))]
public void UserTestEmpty()
{
User target = new User("");
}
[TestMethod()]
[ExpectedException(typeof(ArgumentException))]
public void UserTestBlank()
{
User target = new User(" ");
}
}
}
并将User类更改为
namespace UnitTest2
{
public class User
{
public User(string userEmail)
{
m_email = userEmail;
if (!m_email.Contains("@"))
{
throw new ArgumentException();
}
}
private string m_email; //user email as user id
}
}
运行测试,可得到如图的测试结果
然后我们尝试查看代码覆盖率结果,得到为空结果,如下图所示。
这个结果明显是异常的,通过查阅Microsoft官方的Visual Studio文档,我们找到了这样的表述
要求
代码覆盖率功能仅在 Visual Studio Enterprise 版本中可用。
可以看出只有企业版的Visual Studio才支持使用代码覆盖率这一功能,所以我们所使用的社区版得到了空的结果。
2.2 Java单元测试
在Eclipse IDE环境中,我们可以使用JUnit工具来进行单元测试。
首先从JUnit的Github页面下载JUnit的jar文件,并将其导入到Eclipse IDE的环境中。
然后编写用来测试的Java类
public class Calculate
{
public int add(int a, int b) {
return a + b;
}
public int substract(int a, int b) {
return a - b;
}
public int multiply(int a, int b) {
return a * b;
}
public int divide(int a, int b) {
// 这里只是最简单的测试demo,暂不做容错处理
return a / b;
}
}
2.2.1 手动编写测试类
编写测试类
import static org.junit.Assert.*;
import org.junit.Test;
import Coding.Calculate;
public class CalculateTest {
@Test
public void add()
{
assertEquals(8, new Calculate().add(3, 5));
}
}
运行测试类,得到如下图的结果
2.2.2 自动生成测试类
在被测试类Calculate上单击右键,新建一个其他(Other…)项目
搜索JUnit并新建一个JUnit Test Case
将其存放在UnitTest/test
目录下,并命名为CalculateAutoTest
选择要测试的方法
得到自动生成的测试方法
此时自动生成的测试方法还未编写具体实现,因此无法直接运行,我们将其做如下修改
import static org.junit.Assert.*;
import org.junit.Test;
public class CalculateAutoTest {
@Test
public void testAdd() {
assertEquals(8, new Calculate().add(3, 5));
//fail("Not yet implemented");
}
@Test
public void testSubstract() {
assertEquals(5, new Calculate().substract(6, 1));
//fail("Not yet implemented");
}
@Test
public void testMultiply() {
assertEquals(16, new Calculate().multiply(2, 8));
//fail("Not yet implemented");
}
@Test
public void testDivide() {
assertEquals(9, new Calculate().divide(63, 7));
//fail("Not yet implemented");
}
}
然后再次执行测试,得到如下图所示的结果
Reproduced in: https: //www.cnblogs.com/martinzhang98/p/10696694.html