nunit quick guide
DESCRIPTION
Nunit Quick Guide. 목 차. Assert.That. Assert.That(bool) Assert.That(bool, string) Assert.That(bool, string, params object[]). Assert.AreEqual. Assert.AreEqual(expected, actual [, string message]) Assert.AreEqual(expected, actual, tolerance [, string message]) - PowerPoint PPT PresentationTRANSCRIPT
Nunit Quick Guide
23年 4月 22日
목 차
OS2 강의교재 2
Assert.That(bool)Assert.That(bool, string)Assert.That(bool, string, params object[])
Assert.That
Mona’s C# 3
Assert.AreEqual(expected, actual [, string message])Assert.AreEqual(expected, actual, tolerance [, string message])
실수형 표기는 한계가 있음 . Tolerance 는 그 오차허용치를 지정함그렇지 않으면 모두 실패로 뜸
Assert.AreEqual
Mona’s C# 4
Assert.Less(x, y)Assert.Greater(x, y)
Mona’s C# 5
Assert.LessOrEqual(x, y)Assert.GreaterOrEqual(x, y)
Mona’s C# 6
Assert.IsNull(object [,string message])Assert.IsNotNull(object [,string message])
Mona’s C# 7
Assert.AreSame(expected, actual [,string message])
Mona’s C# 8
Assert.IsTrue(bool condition [,string message])Assert.IsFalse(bool condition [,string message])
Mona’s C# 9
Assert.Fail([string message])
Mona’s C# 10
Contraint-based Assert
Mona’s C# 11
Nunit.Framework.SyntaxHelpersIs.EqualTo()
Assert.That(actual, Is.EqualTo(expected))Is.Not.EqualTo()
Assert.That(actual, Is.Not.EqualTo(expected))Is.AtMost()
Assert.That(actual, Is.AtMost(expected))Is.LessThenOrEqualTo() 의 Alias method
Is.Null()Assert.That(expected, Is.Null);
Is.Not.Null()Assert.That(expected, Is.Not.Null);Assert.That(expected, !Is.Null);
Mona’s C# 12
Is.Empty()Assert.That(expected, Is.Empty);
Is.AtLeast()Assert.That(actual, Is.AtLeast(expected);
Is.InstanceOfTypeAssert.That(actual, Is.InstanceOfType(expected));
Has.Length()Assert.That(actual, Has.Length(expected));
Mona’s C# 13
nunit-console library.dll /fixture:ClassName
nunit-console
Mona’s C# 14
TSP tsp;
[Setup]public void Setup(){ tsp = new TSP();}
Setup attribute
Mona’s C# 15
[Category(“Long”)]Public void UseCities(){ Assert.That(tsp.ShortestPath(5), Is.AtMost(140));}
Category
Mona’s C# 16
Mona’s C# 17
Setup
Test
TearDown
[TestFixtureSetUp][TestFixtureTearDown]클래스 단위로 설정하고 정리하는 작업이 필요할 때
Mona’s C# 18
List.ContainsAssert.That({1,2,3}, List.Contains(2));
Is.SubsetOfAssert.That(new byte[]{1,2,3}, IsSubsetOf(new byte[]{ 1,2,3,4,5});
Text.StartsWithAssert.That(“header:data.”, Text.StartsWith(“header:”));Assert.That(“header:data.”, Text.StartsWith(“headeR:”).IgnoreCase);
Text.MatchesAssert.That(“header:data.”, Text.Matches(@”header.*\.”));
Mona’s C# 19
FileAssert.AreEqual/AreNotEqualFileAssert.AreEqual(FileInfo expected, FileInfo actual)FileAssert.AreEqual(string pathToExpected, string pathToActual)Ex. 스트림이 바이트 단위로 일치하는지 확인
FileAssert
Mona’s C# 20
Stream expectedStream = File.OpenRead(“expected.bin”);Stream actualStream = File.OpenRead(“actual.bin”);Assert.That( actualStream, isEqualTo(expectedStream) );
Custom Assert
Mona’s C# 21
using System;using NUnit.Framework;using NUnit.Framework.SyntaxHelpers;
public class MoneyAssert{// Assert that the amount of money is an even// number of dollars (no cents)public static void AssertNoCents(Money amount,String message){Assert.That(Decimal.Truncate(amount.AsDecimal()),Is.EqualTo(amount.AsDecimal()),message);}// Assert that the amount of money is an even// number of dollars (no cents)public static void AssertNoCents(Money amount){AssertNoCents(amount, String.Empty);}}
test to ensure that the exception is thrown as expected.[Test]public void NullList(){try{WhitePages.ImportList(null);Assert.Fail("ArgumentNullException should have been thrown" );}catch (ArgumentNullException){}}
Testing Exception
Mona’s C# 22
[TestFixture]public class ImportListTests{[Test][ExpectedException(typeof(ArgumentNullException))]public void NullList() {WhitePages.ImportList(null);// Shouldn’t get to here}}
Mona’s C# 23
[Test][Ignore("Out of time. Will Continue Monday. --AH" )]public void Something(){xxx xxx xxxxxx xxxxx xxxx;}ExceptionTest.csNUnit will report
Ignoring Test
Mona’s C# 24
[Test][Platform(Exclude = "Mono" )]public void RemoveOnEmpty() {xxx xx xxx xxxxx xx xx xxx;}[Test, Platform(Exclude = "Net-1.0,Win95" )]public void EmptyStatusBar() {xxx xx xxx xxxxx xx xx xxx;}
Ignoring Platform Dependent Test
Mona’s C# 25
Right BICEPRight: Right Results?B: Boundary conditionI: check Inverse RelationshipsC: Cross-check results using other meansE: force Error conditions to happenP: Performance characteristics within bounds
What To Test?
Mona’s C# 26
Using Data Files
Mona’s C# 27
using NUnit.Framework;using NUnit.Framework.SyntaxHelpers;using System;using System.IO;using System.Collections.Generic;[TestFixture]public class LargestDataFileTests{private int[] getNumberList(string line){string[] tokens = line.Split(null);List<int> numberList = new List<int>();for (int i=1; i < tokens.Length; i++){numberList.Add(Int32.Parse(tokens[i]));}return numberList.ToArray();}
Mona’s C# 28
private int getLargestNumber(string line){string[] tokens = line.Split(null);string val = tokens[0];int expected = Int32.Parse(val);return expected;}private bool hasComment(string line){return line.StartsWith("#" );}
Mona’s C# 29
// Run all the tests in testdata.txt (does not test// exception case). We’ll get an error if any of the// file I/O goes wrong.[Test]public void FromFile(){string line;// most IDEs output the test bi-nary in bin/[Debug,Release]StreamReader reader =new StreamReader("../../testdata.txt" );while ((line = reader.ReadLine()) != null){if (hasComment(line)){continue;}int[] numberListForLine = getNumberList(line);int expectedLargestNumber = getLargestNumber(line);int actualLargestNumber = Cmp.Largest(numberListForLine));Assert.That(expectedLargestNumber, Is.EqualTo(actualLargestNumber));}}
Test File Format
Mona’s C# 30
## Simple tests:#9 7 8 99 9 8 79 9 8 9## Negative number tests:#-7 -7 -8 -9-7 -8 -7 -8-7 -9 -7 -8## Mixture:#7 -9 -7 -8 7 6 49 -1 0 9 -7 4## Boundary conditions:#1 10 02147483647 2147483647-2147483648 -2147483648
Most Bugs happen hereMax, Min values, boundary in arrays, lists• Totally bogus or inconsistent input values, such as a filename of "!*W:X\&Gi/w>g/h#WQ@".• Badly formatted data that is missing delimeters or terminators,such as an e-mail address without a top-leveldomain ("fred@foobar.").2• Empty or missing values (such as 0, 0.0, an empty string,an empty array, or null), or missing in a sequence (suchas a missing TCP packet).• Values far in excess of reasonable expectations, such asa person’s age of 10,000 years or a password string with10,000 characters in it.• Duplicates in lists that shouldn’t have duplicates.• Ordered lists that aren’t, and vice-versa. Try handing apre-sorted list to a sort algorithm, for instance—or evena reverse-sorted list.• Things that arrive out of order, or happen out of expectedorder, such as trying to print a document before loggingin, or getting fragmented IP packets out of order, for instance.
Boundary Conditions
Mona’s C# 31
Conformance — Does the value conform to an expectedformat?• Ordering — Is the set of values ordered or unordered as appropriate?• Range — Is the value within reasonable minimum andmaximum values?• Reference — Does the code reference anything externalthat isn’t under direct control of the code itself?• Existence — Does the value exist (e.g., is non-null, nonzero,present in a set, etc.)?• Cardinality — Are there exactly enough values?• Time (absolute and relative) — Is everything happeningin order? At the right time? In time?
CORRECT in Boundary Conditions
Mona’s C# 32
Format 을 따르나 ?Email 주소 : [email protected]
• What if there’s no header, just data and a trailer?• What if there’s no data, just a header and trailer?• What if there’s no trailer, just a header and data?• What if there’s just a trailer?• What if there’s just a header?• What if there’s just data?
Conformance
Mona’s C# 33
순서가 있는 데이터 집합
Ordering
Mona’s C# 34
데이터 종류에 따른 범위나이 , 각도 (angle)
데이터 타입에 따른 범위Int, long 형
Range
Mona’s C# 35
public const int MAX_DIST = 100;static public void AssertPairInRange(Point one,Point two,String message){Assert.That(Math.Abs(one.X - two.X),Is.AtMost(MAX_DIST),message);Assert.That(Math.Abs(one.Y - two.Y),Is.AtMost(MAX_DIST),message);}
Range -2
Mona’s C# 36
public class MyStack{public MyStack(){elements = new string[100];nextIndex = 0;}public String Pop(){return elements[--nextIndex];}// Delete n items from the elements en-massepublic void Delete(int n){nextIndex -= n;}public void Push(string element){elements[nextIndex++] = element;}public String Top(){return elements[nextIndex-1];}private int nextIndex;private string[] elements;}
Mona’s C# 37
public void CheckInvariant(){if (!(nextIndex >= 0 &&nextIndex < elements.Length)){throw new InvariantException("nextIndex out of range: " + nextIndex +" for elements length " + elements.Length);}}MyStack.cs
Mona’s C# 38
using NUnit.Framework;[TestFixture]public class MyStackTest{[Test]public void Empty(){MyStack stack = new MyStack();stack.CheckInvariant();stack.Push("sample" );stack.CheckInvariant();// Popping last element okAssert.That(stack.Pop(),Is.EqualTo("sample" ));stack.CheckInvariant();// Delete from empty stackstack.Delete(1);stack.CheckInvariant();}}
Mona’s C# 39
• Start and End index have the same value• First is greater than Last• Index is negative• Index is greater than allowed• Count doesn’t match actual number of items
Mona’s C# 40
역관계가 성립하는지 확인역으로 계산하는 것도 맞는지 확인반대 관계반대 순서로 했을 때 원 순서가 유지되는지 테스트
Inverse Relationships
Mona’s C# 41
[Test]public void SquareRootUsingInverse() {double x = MyMath.SquareRoot(4.0);Assert.That(4.0, Is.EqualTo(x*x).Within(0.0001));}
계산을 반대로 해보기DB 에서 삽입 <-> 삭제부드러운 이미지 <-> 선명한 이미지
Inverse Relationships Examples
Mona’s C# 42
• Running out of memory• Running out of disk space• Issues with wall-clock time• Network availability and errors• Insufficient File or Path permissions• System load• Limited color palette• Very high or very low video resolution
Force Error Conditions
Mona’s C# 43
데이터 크기에 따른 성능 특성 테스트작은 데이터 집단큰 데이터 집단
Performance Characteristics
Mona’s C# 44
Mona’s C# 45
일반원칙망가질 가능성이 있는 모든 것을 테스트한다 .망가지는 모든 것을 테스트한다 .새 코드는 무죄가 증명되기 전까지는 유죄 .적어도 제품 코드만큼 테스트 코드를 작성한다 .컴파일을 할 때마다 지역 테스트를 실행한다 .저장소에 체크인하기 전에 모든 테스트를 실행해본다 .무엇을 테스트해야 하는가 RIGHT-BICEPRight : 결과가 옳은가 ?Boundary : 모든 경계 조건이 CORRECT( 아래 참조 ) 한가 ?Inverse : 역관계를 확인할 수 있는가 ?Cross-check : 다른 수단을 사용해서 결과를 교차 확인 할 수 있는가 ?Error condition : 에러조건을 강제로 만들어 낼 수 있는가 ?Perfomance : 성능 특성이 한도내에 있는가 ?CORRECT 경계 조건Conformance : 예상한 형식과 일치하는가 ?Ordering : 적절한 순서대로 되어있는가 ?Range : 최소값과 최대값사이에 있는가 ?Reference : 외부코드를 참조 하는가 ?Existence : 값이 존재하는가 ?Cardinality : 확실히 충분한 값이 존재하는가 ?Time : 제시간에 때맞추어 일어나는가 ?좋은 테스트는 A-TRIP 해야 한다 .Automatic : 자동적이어야 한다 .Through : 철저해야 한다 .Repeatable : 반복가능해야 한다 .Independent : 독립적이어야 한다 .Professional : 전문적이어야 한다 .
Mona’s C# 46