Xóa bài viết
Bạn có chắc chắn muốn xóa bài viết này không ?
Xóa bình luận
Bạn có chắc chắn muốn xóa bình luận này không ?
Unit Test example in C# - nUnit
Đây không phải bài hướng dẫn mà chỉ là một bài note cá nhân.
Using TDD (Test Driven Development), write Unit Test first and implement function to pass unit test later.
Framework: nUnit - Nuget Package: nUnit, nUnitTest3Adapter
Instance class
public class LRU
{
}
Test Class
Creation Test
[TestFixture] // nUnit Attribute for classes
public class LRUTest
{
[Test] //nUnit Attribute for methods
// First Test - Test creation
public void TestDummy()
{
lru instance = new lru();
Assert.That(instance, Is.Not.Null);
}
}
Make List when Constructor called Test
[Test]
public void WhenCtorCalledThenListShouldBeCreated()
{
lru storage = new lru();
Assert.That(storage.Recent, Is.Not.Null);
Assert.That(storage.Recent, Is.Empty);
}
Change instance class
public List<object> Recent { get; private set; }
public lru()
{
Recent = new List<object>();
}
Test List Limit with Parameter
[TestCase(10)] // success
[TestCase(199)] // failed
public void TestDefaultParam(int limit)
{
lru storage = new lru();
Assert.That(storage.ListLimit, Is.EqualTo(limit));
}
Change instance class
public List<object> Recent { get; private set; }
public const int defaultLimit = 10; // using later
public int ListLimit { get; private set; }
public lru(int limit = defaultLimit)
{
ListLimit = limit;
Recent = new List<object>();
}
Making Setup for later purpose
const int limit = 5; // test limit
lru storage;
[SetUp]
public void Setup()
{
storage = new lru(limit);
}
Add Null then throw exception Test
[Test]
public void TestAddNullThrowsException()
{
Assert.That(() => storage.Add(null), Throws.ArgumentNullException);
}
Change instance class
public void Add(object subject)
{
if(subject == null)
{
throw new ArgumentNullException(nameof(subject));
}
Recent.Add(subject);
}
Test the test limit Test - Make sure List contain only 5
[Test]
public void TestLimitWorks()
{
object[] instances = new object[2 * limit];
for (int i = 0; i < instances.Length; i++)
{
instances[i] = new { idx = i };
storage.Add(instances[i]);
}
Assert.That(storage.Recent.Count, Is.EqualTo(limit)); // Count same limit or not - will fail if not debug
for (int i = 0; i < limit; i++)
{
Assert.That(storage.Recent, Does.Contain(instances[i + limit])); // contain element from index 6 and above
}
}
Debug instance class
public void Add(object subject)
{
if(subject == null)
{
throw new ArgumentNullException(nameof(subject));
}
Recent.Add(subject);
if (Recent.Count > ListLimit)
{
Recent.RemoveAt(0); // remove if above and remove from the first
}
}
Test number of element in list and remove the dupplicate object
static IEnumerable<TestCaseData> TestCountSource
{
get
{
object first = new { idx = 1 };
object second = new { idx = 2 };
object third = new { idx = 3 };
List<TestCaseData> output = new List<TestCaseData>();
output.Add(new TestCaseData(new object[] { first, second, third }, 3));
output.Add(new TestCaseData(new object[] { first, second, second }, 2));
output.Add(new TestCaseData(new object[] { first, first, first }, 1));
return output;
}
}
[TestCaseSource(nameof(TestCountSource))]
public void TestCount(object[] instances, int expectedCount)
{
foreach (object item in instances)
{
storage.Add(item);
}
Assert.That(storage.Recent.Count, Is.EqualTo(expectedCount));
}
Debug instance class
public void Add(object subject)
{
if(subject == null)
{
throw new ArgumentNullException(nameof(subject));
}
if(Recent.Contains(subject))
{
Recent.Remove(subject);
}
Recent.Add(subject);
if (Recent.Count > ListLimit)
{
Recent.RemoveAt(0);
}
}
Test Reverse order Test
static IEnumerable<TestCaseData> TestOrderSource
{
get
{
object first = new { idx = 1 };
object second = new { idx = 2 };
object third = new { idx = 3 };
List<TestCaseData> output = new List<TestCaseData>();
output.Add(new TestCaseData(new object[] { first, second, third }, new object[] { third, second, first }));
output.Add(new TestCaseData(new object[] { first, second, second }, new object[] { second, first }));
output.Add(new TestCaseData(new object[] { first, first, first }, new object[] { first}));
return output;
}
}
[TestCaseSource(nameof(TestOrderSource))]
public void TestOrder(object[] instances, object[] expectedOrder)
{
foreach(object item in instances)
{
storage.Add(item);
}
for (int i = 0; i < expectedOrder.Length; i++)
{
Assert.That(storage.Recent[i], Is.SameAs(expectedOrder[i]));
}
}
Debug - make function instance class
public void Add(object subject)
{
if(subject == null)
{
throw new ArgumentNullException(nameof(subject));
}
if(Recent.Contains(subject))
{
Recent.Remove(subject);
}
//Recent.Add(subject);
Recent.Insert(0, subject);
if (Recent.Count > ListLimit)
{
Recent.RemoveAt(0);
}
}
Now every test worked fin... wait
Accidently make a new bug ...
Life isn't easy ...
Full github repo: https://github.com/PhiHuyHoang/Programming3/tree/master/LRU.Logic
Bình luận

{{ comment.user.name }}
Bỏ hay
Hay

Cùng một tác giả

17
8
Sản phẩm sau khi làm http://coffeetube.herokuapp.com/ Yêu cầu Nói chung thì giờ download nhạc từ youtube thì nó có vô số cách rồi. Nhưng tự mình...

9
5
Trước giờ logic code của mình vẫn luôn dễ dãi như gái làng chơi nên đôi khi nó đã support thêm cho mình cái đức tính càng lúc càng không (thèm) kiê...

7
7
Description Mình là một thằng thích đọc sách. Nhưng lúc nào cũng bận (lười) nên cũng mấy tháng rồi chưa hoàn thành được quyển sách nào. Mình đa số...
Bài viết liên quan

0
0
Mình khá là lười, nên mình sẽ không đưa định nghĩa hay usage của reflection vào đây. Vì dù sao mình cũng sẽ chỉ copy thôi :smile:. Vậy nên chúng ta...