Nunit Quick Guide 2020 11 05 Assert Thatbool

Nunit Quick Guide 2020 -11 -05

![Assert. That(bool) Assert. That(bool, string, params object[]) Mona’s C# 3 Assert. That(bool) Assert. That(bool, string, params object[]) Mona’s C# 3](http://slidetodoc.com/presentation_image/56f7a9e33179f87d9537909a3fc39c41/image-3.jpg)
Assert. That(bool) Assert. That(bool, string, params object[]) Mona’s C# 3
![Assert. Are. Equal(expected, actual [, string message]) Assert. Are. Equal(expected, actual, tolerance [, string Assert. Are. Equal(expected, actual [, string message]) Assert. Are. Equal(expected, actual, tolerance [, string](http://slidetodoc.com/presentation_image/56f7a9e33179f87d9537909a3fc39c41/image-4.jpg)
Assert. Are. Equal(expected, actual [, string message]) Assert. Are. Equal(expected, actual, tolerance [, string message]) 실수형 표기는 한계가 있음. Tolerance는 그 오차허용치를 지정 함 그렇지 않으면 모두 실패로 뜸 Mona’s C# 4

Assert. Less(x, y) Assert. Greater(x, y) Mona’s C# 5

Assert. Less. Or. Equal(x, y) Assert. Greater. Or. Equal(x, y) Mona’s C# 6
![Assert. Is. Null(object [, string message]) Assert. Is. Not. Null(object [, string message]) Mona’s Assert. Is. Null(object [, string message]) Assert. Is. Not. Null(object [, string message]) Mona’s](http://slidetodoc.com/presentation_image/56f7a9e33179f87d9537909a3fc39c41/image-7.jpg)
Assert. Is. Null(object [, string message]) Assert. Is. Not. Null(object [, string message]) Mona’s C# 7
![Assert. Are. Same(expected, actual [, string message]) Mona’s C# 8 Assert. Are. Same(expected, actual [, string message]) Mona’s C# 8](http://slidetodoc.com/presentation_image/56f7a9e33179f87d9537909a3fc39c41/image-8.jpg)
Assert. Are. Same(expected, actual [, string message]) Mona’s C# 8
![Assert. Is. True(bool condition [, string message]) Assert. Is. False(bool condition [, string message]) Assert. Is. True(bool condition [, string message]) Assert. Is. False(bool condition [, string message])](http://slidetodoc.com/presentation_image/56f7a9e33179f87d9537909a3fc39c41/image-9.jpg)
Assert. Is. True(bool condition [, string message]) Assert. Is. False(bool condition [, string message]) Mona’s C# 9
![Assert. Fail([string message]) Mona’s C# 10 Assert. Fail([string message]) Mona’s C# 10](http://slidetodoc.com/presentation_image/56f7a9e33179f87d9537909a3fc39c41/image-10.jpg)
Assert. Fail([string message]) Mona’s C# 10

Contraint-based Assert Mona’s C# 11

Nunit. Framework. Syntax. Helpers Is. Equal. To() Assert. That(actual, Is. Equal. To(expected)) Is. Not. Equal. To() Assert. That(actual, Is. Not. Equal. To(expected)) Is. At. Most() Assert. That(actual, Is. At. Most(expected)) Is. Less. Then. Or. Equal. To()의 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. At. Least() Assert. That(actual, Is. At. Least(expected); Is. Instance. Of. Type Assert. That(actual, Is. Instance. Of. Type(expected)); Has. Length() Assert. That(actual, Has. Length(expected)); Mona’s C# 13

nunit-console library. dll /fixture: Class. Name Mona’s C# 14
![Setup attribute TSP tsp; [Setup] public void Setup() { tsp = new TSP(); } Setup attribute TSP tsp; [Setup] public void Setup() { tsp = new TSP(); }](http://slidetodoc.com/presentation_image/56f7a9e33179f87d9537909a3fc39c41/image-15.jpg)
Setup attribute TSP tsp; [Setup] public void Setup() { tsp = new TSP(); } Mona’s C# 15
![Category [Category(“Long”)] Public void Use. Cities() { Assert. That(tsp. Shortest. Path(5), Is. At. Most(140)); Category [Category(“Long”)] Public void Use. Cities() { Assert. That(tsp. Shortest. Path(5), Is. At. Most(140));](http://slidetodoc.com/presentation_image/56f7a9e33179f87d9537909a3fc39c41/image-16.jpg)
Category [Category(“Long”)] Public void Use. Cities() { Assert. That(tsp. Shortest. Path(5), Is. At. Most(140)); } Mona’s C# 16

Setup Test Tear. Down Mona’s C# 17

![List. Contains Assert. That({1, 2, 3}, List. Contains(2)); Is. Subset. Of Assert. That(new byte[]{1, List. Contains Assert. That({1, 2, 3}, List. Contains(2)); Is. Subset. Of Assert. That(new byte[]{1,](http://slidetodoc.com/presentation_image/56f7a9e33179f87d9537909a3fc39c41/image-19.jpg)
List. Contains Assert. That({1, 2, 3}, List. Contains(2)); Is. Subset. Of Assert. That(new byte[]{1, 2, 3}, Is. Subset. Of(new byte[]{ 1, 2, 3, 4, 5}); Text. Starts. With Assert. That(“header: data. ”, Text. Starts. With(“header: ”)); Assert. That(“header: data. ”, Text. Starts. With(“heade. R: ”). Ignore. Case); Text. Matches Assert. That(“header: data. ”, Text. Matches(@”header. *. ”)); Mona’s C# 19

File. Assert. Are. Equal/Are. Not. Equal File. Assert. Are. Equal(File. Info expected, File. Info actual) File. Assert. Are. Equal(string path. To. Expected, string path. To. Actual) Ex. 스트림이 바이트 단위로 일치하는지 확인 Stream expected. Stream = File. Open. Read(“expected. bin”); Stream actual. Stream = File. Open. Read(“actual. bin”); Assert. That( actual. Stream, is. Equal. To(expected. Stream) ); Mona’s C# 20

Custom Assert using System; using NUnit. Framework. Syntax. Helpers; public class Money. Assert { // Assert that the amount of money is an even // number of dollars (no cents) public static void Assert. No. Cents(Money amount, String message) { Assert. That( Decimal. Truncate(amount. As. Decimal()), Is. Equal. To(amount. As. Decimal()), message); } // Assert that the amount of money is an even // number of dollars (no cents) public static void Assert. No. Cents(Money amount) { Assert. No. Cents(amount, String. Empty); } } Mona’s C# 21
![Testing Exception test to ensure that the exception is thrown as expected. [Test] public Testing Exception test to ensure that the exception is thrown as expected. [Test] public](http://slidetodoc.com/presentation_image/56f7a9e33179f87d9537909a3fc39c41/image-22.jpg)
Testing Exception test to ensure that the exception is thrown as expected. [Test] public void Null. List() { try { White. Pages. Import. List(null); Assert. Fail("Argument. Null. Exception should have been thrown" ); } catch (Argument. Null. Exception) { } } Mona’s C# 22
![[Test. Fixture] public class Import. List. Tests { [Test] [Expected. Exception(typeof(Argument. Null. Exceptio n))] [Test. Fixture] public class Import. List. Tests { [Test] [Expected. Exception(typeof(Argument. Null. Exceptio n))]](http://slidetodoc.com/presentation_image/56f7a9e33179f87d9537909a3fc39c41/image-23.jpg)
[Test. Fixture] public class Import. List. Tests { [Test] [Expected. Exception(typeof(Argument. Null. Exceptio n))] public void Null. List() { White. Pages. Import. List(null); // Shouldn’t get to here } } Mona’s C# 23
![Ignoring Test [Test] [Ignore("Out of time. Will Continue Monday. -AH" )] public void Something() Ignoring Test [Test] [Ignore("Out of time. Will Continue Monday. -AH" )] public void Something()](http://slidetodoc.com/presentation_image/56f7a9e33179f87d9537909a3fc39c41/image-24.jpg)
Ignoring Test [Test] [Ignore("Out of time. Will Continue Monday. -AH" )] public void Something() { xxx xxxxxx; } Exception. Test. cs NUnit will report Mona’s C# 24
![Ignoring Platform Dependent Test [Test] [Platform(Exclude = "Mono" )] public void Remove. On. Empty() Ignoring Platform Dependent Test [Test] [Platform(Exclude = "Mono" )] public void Remove. On. Empty()](http://slidetodoc.com/presentation_image/56f7a9e33179f87d9537909a3fc39c41/image-25.jpg)
Ignoring Platform Dependent Test [Test] [Platform(Exclude = "Mono" )] public void Remove. On. Empty() { xxx xxxxx xxx; } [Test, Platform(Exclude = "Net-1. 0, Win 95" )] public void Empty. Status. Bar() { xxx xxxxx xxx; } Mona’s C# 25

What To Test? Right BICEP Right: Right Results? B: Boundary condition I: check Inverse Relationships C: Cross-check results using other means E: force Error conditions to happen P: Performance characteristics within bounds Mona’s C# 26

Using Data Files using NUnit. Framework; using NUnit. Framework. Syntax. Helpers; using System. IO; using System. Collections. Generic; [Test. Fixture] public class Largest. Data. File. Tests { private int[] get. Number. List(string line) { string[] tokens = line. Split(null); List<int> number. List = new List<int>(); for (int i=1; i < tokens. Length; i++) { number. List. Add(Int 32. Parse(tokens[i])); } return number. List. To. Array(); } Mona’s C# 27
![private int get. Largest. Number(string line) { string[] tokens = line. Split(null); string val private int get. Largest. Number(string line) { string[] tokens = line. Split(null); string val](http://slidetodoc.com/presentation_image/56f7a9e33179f87d9537909a3fc39c41/image-28.jpg)
private int get. Largest. Number(string line) { string[] tokens = line. Split(null); string val = tokens[0]; int expected = Int 32. Parse(val); return expected; } private bool has. Comment(string line) { return line. Starts. With("#" ); } Mona’s C# 28

// 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 From. File() { string line; // most IDEs output the test binary in bin/[Debug, Release] Stream. Reader reader = new Stream. Reader(". . /testdata. txt" ); while ((line = reader. Read. Line()) != null) { if (has. Comment(line)) { continue; } int[] number. List. For. Line = get. Number. List(line); int expected. Largest. Number = get. Largest. Number(line); int actual. Largest. Number = Cmp. Largest(number. List. For. Line)); Assert. That(expected. Largest. Number, Is. Equal. To(actual. Largest. Number)); } } Mona’s C# 29

Test File Format # # Simple tests: # 9789 9987 9989 # # Negative number tests: # -7 -7 -8 -9 -7 -8 -7 -9 -7 -8 # # Mixture: # 7 -9 -7 -8 7 6 4 9 -1 0 9 -7 4 # # Boundary conditions: # 11 00 2147483647 -2147483648 Mona’s C# 30

Boundary Conditions Most Bugs happen here Max, Min values, boundary in arrays, lists • Totally bogus or inconsistent input values, such as a file name 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-level domain ("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 (such as a missing TCP packet). • Values far in excess of reasonable expectations, such as a person’s age of 10, 000 years or a password string with 10, 000 characters in it. • Duplicates in lists that shouldn’t have duplicates. • Ordered lists that aren’t, and vice-versa. Try handing a pre-sorted list to a sort algorithm, for instance—or even a reverse-sorted list. • Things that arrive out of order, or happen out of expected order, such as trying to print a document before logging in, or getting fragmented IP packets out of order, for instance. Mona’s C# 31

CORRECT in Boundary Conditions Conformance — Does the value conform to an expected format? • Ordering — Is the set of values ordered or unordered as appropriate? • Range — Is the value within reasonable minimum and maximum values? • Reference — Does the code reference anything external that 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 happening in order? At the right time? In time? Mona’s C# 32

Conformance Format을 따르나? Email 주소: myid@ring. net • What trailer? • What data? • What if there’s no header, just data and a if there’s no data, just a header and if there’s no trailer, just a header and if there’s just a trailer? if there’s just a header? if there’s just data? Mona’s C# 33



Range -2 public const int MAX_DIST = 100; static public void Assert. Pair. In. Range(Point one, Point two, String message) { Assert. That( Math. Abs(one. X - two. X), Is. At. Most(MAX_DIST), message ); Assert. That( Math. Abs(one. Y - two. Y), Is. At. Most(MAX_DIST), message ); } Mona’s C# 36
![public class My. Stack { public My. Stack() { elements = new string[100]; next. public class My. Stack { public My. Stack() { elements = new string[100]; next.](http://slidetodoc.com/presentation_image/56f7a9e33179f87d9537909a3fc39c41/image-37.jpg)
public class My. Stack { public My. Stack() { elements = new string[100]; next. Index = 0; } public String Pop() { return elements[--next. Index]; } // Delete n items from the elements en-masse public void Delete(int n) { next. Index -= n; } public void Push(string element) { elements[next. Index++] = element; } public String Top() { return elements[next. Index-1]; } private int next. Index; private string[] elements; } Mona’s C# 37

public void Check. Invariant() { if (!(next. Index >= 0 && next. Index < elements. Length)) { throw new Invariant. Exception( "next. Index out of range: " + next. Index + " for elements length " + elements. Length); } } My. Stack. cs Mona’s C# 38
![using NUnit. Framework; [Test. Fixture] public class My. Stack. Test { [Test] public void using NUnit. Framework; [Test. Fixture] public class My. Stack. Test { [Test] public void](http://slidetodoc.com/presentation_image/56f7a9e33179f87d9537909a3fc39c41/image-39.jpg)
using NUnit. Framework; [Test. Fixture] public class My. Stack. Test { [Test] public void Empty() { My. Stack stack = new My. Stack(); stack. Check. Invariant(); stack. Push("sample" ); stack. Check. Invariant(); // Popping last element ok Assert. That( stack. Pop(), Is. Equal. To("sample" ) ); stack. Check. Invariant(); // Delete from empty stack. Delete(1); stack. Check. Invariant(); } } 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 역관계가 성립하는지 확인 역으로 계산하는 것도 맞는지 확인 반대 관계 반대 순서로 했을 때 원 순서가 유지되는지 테스트 [Test] public void Square. Root. Using. Inverse() { double x = My. Math. Square. Root(4. 0); Assert. That(4. 0, Is. Equal. To(x*x). Within(0. 0001)); } Mona’s C# 41


Force Error Conditions • • 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 Mona’s C# 43


Mona’s C# 45

- Slides: 46