Opem XTerm and type
bash$ sudo passwd root
Then type a new password for root.
That's all!
Opem XTerm and type
bash$ sudo passwd root
Let's start unit-test journey using this simple class, in which we want to test whether this class returns all passes or failures.
using System;
using System.Collections.Generic;
using System.Text;
namespace UnitTest
{
enum CalculatingType
{
Addition,
Subtraction,
Multiplication,
Division
}
class Calculator
{
private List List;
private CalculatingType cType;
public Calculator() { }
public Calculator(List list, CalculatingType type)
{
this.List = list;
this.cType = type;
}
public List CList
{
get { return this.List; }
set { this.List = value; }
}
public CalculatingType CType
{
get { return this.cType; }
set { this.cType = value; }
}
public double GetResult()
{
double result = this.List[0];
for (int i = 1; i < this.List.Count; ++i)
{
if (cType == CalculatingType.Addition)
result += this.List[i];
else if (cType == CalculatingType.Subtraction)
result -= this.List[i];
else if (cType == CalculatingType.Multiplication)
result *= this.List[i];
else
result /= this.List[i];
}
return result;
}
}
}
That's it, there have 'Calculator' class containing a list of double values and a enumeration type indicating what kind of computing. Right now, you should have installed NUnit before moving on. Let's create a new class named 'CalculatorTest' to test our created-class 'Calculator'. So what do we want to test? It can be, and should be: + constructors + properties + methods Since this class is quite simple, so testing constructors and properties can be considered alike; in other words, we will test properties and methods of class 'Calculator' In the current project, add a new class named 'CalculatorTest'. Add Reference to NUnit.Framework.Dll, which is located in installed NUnit directory. First of our test code using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
namespace UnitTest
{
[TestFixture]
class CalculatorTest
{
[Test]
public void TestCListA()
{
Calculator calc = new Calculator(new List { 1, 2, 3 }, CalculatingType.Addition);
Assert.AreEqual(new List { 1, 2, 3 }, calc.CList);
}
}
}
[Test]
public void TestCListB()
{
Calculator calc = new Calculator(new List { 1, 2, 3 }, CalculatingType.Addition);
Assert.AreEqual(new List { 2, 3, 1 }, calc.CList);
}
UnitTest.CalculatorTest.TestCListB: Expected and actual are bothSo that means the numbers at index 0 are different. We may understand like both lists containing the same number but arranged in different order, but in facts, it isn't; they're compared one-by-one iterate through index of each list.with 3 elements Values differ at index [0] Expected: 2.0d But was: 1.0d
[Test]
public void TestCListC()
{
Calculator calc = new Calculator(new List { 1, 2, 3 }, CalculatingType.Addition);
Assert.AreEqual(new List { -1, 2, 3 }, calc.CList);
}
[Test]
public void TestCTypeA()
{
Calculator calc = new Calculator(new List { 1, 2, 3 }, CalculatingType.Addition);
Assert.AreEqual(CalculatingType.Addition, calc.CType);
}
[Test]
public void TestCTypeB()
{
Calculator calc = new Calculator(new List { 1, 2, 3 }, CalculatingType.Addition);
Assert.AreEqual(CalculatingType.Subtraction, calc.CType);
}
It's easy to see that TestCTypeA() passes and TestCTypeB() fails logically. [Test]
public void TestAddition()
{
Calculator calc = new Calculator(new List { 1, 2, 3 }, CalculatingType.Addition);
Assert.AreEqual(6, calc.GetResult());
}
[Test]
public void TestSubtraction()
{
Calculator calc = new Calculator(new List { 1, 2, 3 }, CalculatingType.Subtraction);
Assert.AreEqual(-4, calc.GetResult());
}
[Test]
public void TestMultiplication()
{
Calculator calc = new Calculator(new List { 1, 2, 3 }, CalculatingType.Multiplication);
Assert.AreEqual(6, calc.GetResult());
}
[Test]
public void TestDivisionA()
{
Calculator calc = new Calculator(new List { 1, 2, 3 }, CalculatingType.Division);
Assert.AreEqual(0.1666667, calc.GetResult());
}
[Test]
public void TestDivisionB()
{
Calculator calc = new Calculator(new List { 1, 2, 3 }, CalculatingType.Division);
Assert.AreEqual( 0.5/3, calc.GetResult());
}
Good trick! We pass TestDivisionB(). [Test,ExpectedException(typeof(DivideByZeroException))]
public void TestDivisionC()
{
Calculator calc = new Calculator(new List { 1, 2, 0 }, CalculatingType.Division);
throw new DivideByZeroException();
}