public interface ICalculatorIMoneyEx接口定义如下:
{
int Add(int param1, int param2);
int Subtract(int param1, int param2);
int Multipy(int param1, int param2);
double Divide(int param1, int param2);
int ConvertUSDtoRMB(int unit);
}
public interface IMoneyExICaculator接口的实现类Caculator如下:
{
int GetActualUSDValue();
}
public class Calculator : ICalculator测试代码
{
private IMoneyEx _feed;
public Calculator(IMoneyEx feed)
{
this._feed = feed;
}
#region ICalculator Members
public int Add(int param1, int param2)
{
return param1 + param2;
}
public int Subtract(int param1, int param2)
{
return param1 - param2;
}
public int Multipy(int param1, int param2)
{
return param1 * param2;
}
public double Divide(int param1, int param2)
{
return param1 / param2;
}
public int ConvertUSDtoRMB(int unit)
{
return unit * this._feed.GetActualUSDValue();
}
#endregion
}
// 定义mock逻辑不同测试工具测试代码
private IMoneyEx PrvGetMockExchangeRateFeed()
{
Mock<IMoneyEx> mockObject = new Mock<IMoneyEx>();
mockObject.Setup(m => m.GetActualUSDValue()).Returns(500);
return mockObject.Object;
}
namespace CalculatorPkg.TestsDotCover结果显示:
{
private static ICalculator calculator = null;
[TestClass()]
public class MsTestCal
{
// 初始化
[TestInitialize]
public void Setup()
{
IMoneyEx feed = this.PrvGetMockExchangeRateFeed();
calculator = new Calculator(feed);
Console.WriteLine("before test...");
}
//带参测试方法,并对测试方法分类
[TestMethod()]
[DataRow(1,1)]
[TestCategory("cal")]
public void TC1_Add(int a,int b)
{
int c = a + b;
Assert.AreEqual(c, calculator.Add(a,b));
}
// 忽略测试用例
[TestMethod()]
[Ignore]
[TestCategory("cal")]
public void TC1_Divide9By3()
{
double actualResult = calculator.Divide(9, 3);
int expectedResult = 3;
Assert.AreEqual(expectedResult, actualResult);
}
// 异常判断
[TestMethod()]
[ExpectedException(typeof(DivideByZeroException))]
[TestCategory("cal")]
public void TC2_DivideByZero()
{
double actualResult = calculator.Divide(9, 0);
}
// 使用mock类
[TestMethod()]
[TestCategory("convert")]
public void TC3_ConvertUSDtoRMBTest()
{
int actualResult = calculator.ConvertUSDtoRMB(1);
int expectedResult = 500;
Assert.AreEqual(expectedResult, actualResult);
}
// 后处理
[TestCleanup]
public void Cleanup()
{
Console.WriteLine("after test...");
}
}
}
namespace CalculatorPkg.TestsDotCover结果显示:
{
// 添加TestFixture标识类是测试类
[TestFixture]
public class NUnitTest
{
// 初始化
[SetUp]
public void Setup()
{
IMoneyEx feed = this.PrvGetMockExchangeRateFeed();
calculator = new Calculator(feed);
Console.WriteLine("before test...");
}
// 测试用例分类,跳过测试方法
[Test(Description = "Add 1 with 1. Expected result is 2.")]
[Category("add")]
[Ignore("Method is ignored")]
public void TC0_Add1With1()
{
int actualResult = calculator.Add(1,1);
int expectedResult = 2;
Assert.AreEqual(expectedResult, actualResult);
}
// 带参测试方法
[Test()]
[Category("sub")]
[TestCase(0,1)]
[TestCase(1,1)]
[TestCase(2, 1)]
public void TC0_SUB(int a,int b)
{
int actualResult = calculator.Subtract(a,b);
int expectedResult = a-b;
Assert.AreEqual(expectedResult, actualResult);
}
// 测试divide方法,以及测试用例排序
[Test(Description = "Divide 9 by 3. Expected result is 3.")]
[Category("divide")]
[Order(2)]
public void TC1_Divide9By3()
{
Console.WriteLine("TC1_Divide9By3:" + DateTime.Now.TimeOfDay.ToString());
double actualResult = calculator.Divide(9, 3);
int expectedResult = 3;
Assert.AreEqual(expectedResult, actualResult);
}
// 异常判断
[Test(Description = "Divide any number by zero. Should throw an System.DivideByZeroException exception.")]
[Category("divide")]
[Order(3)]
public void TC2_DivideByZero()
{
Console.WriteLine("TC2_DivideByZero:" + DateTime.Now.TimeOfDay.ToString());
Assert.That(() => calculator.Divide(9, 0), Throws.TypeOf<DivideByZeroException>());
}
// mock实例
[Test(Description = "Convert 1 USD to RMB. Expected result is 500.")]
[Category("convert")]
[Order(1)]
public void TC3_ConvertUSDtoRMBTest()
{
Console.WriteLine("TC3_ConvertUSDtoRMBTest执行时间:" + DateTime.Now.TimeOfDay.ToString());
int actualResult = calculator.ConvertUSDtoRMB(1);
int expectedResult = 500;
Assert.AreEqual(expectedResult, actualResult);
}
// 后处理
[TearDown]
public void Teardown()
{
Console.WriteLine("after test...");
}
}
}
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) | Powered by Discuz! X3.2 |