How to expose assembly internals for testing Add following line into AssemblyInfo.cs of the assembly which internals should be exposed
[assembly: InternalsVisibleTo ("MyTestAssemblyName")]
More info and samples on: www.devarchweb.net
How to access private methods and members in unit tests Assuming having a class like this:class ClassToBeTested
{
private bool Test()
{
return true;
}
private int Add(int a, int b)
{
return a+b;
}
private int IntField;
private int IntProperty { get; set; }
}
using Microsoft.VisualStudio.TestTools.UnitTesting; // Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll
...
ClassToBeTested target = new ClassToBeTested();
PrivateObject privObj = new PrivateObject(target);
var retValue = obj.Invoke("Test");
object[] args1 = { 1, 2 };
retValue = obj.Invoke("Add", args1);
obj.SetField("IntField", 1);
retValue = obj.GetField("IntField");
obj.SetProperty("IntProperty", 2);
retValue = obj.GetProperty("IntProperty");
More info and samples on: www.devarchweb.net
How to create object with private constructor Sometimes objects like exceptions may have private constructors. If it is necessary to create such an object e.g. for testing,private OracleException CreateOracleException(string message)
{
ConstructorInfo ci = typeof(OracleException).GetConstructor(
BindingFlags.NonPublic | BindingFlags.Instance,
null,
new Type[] { typeof(string), typeof(int) },
null);
return (OracleException)ci.Invoke(new object[] { "my message", 123 });
}
More info and samples on: www.devarchweb.net
How to get test coverage for NUnit tests 1.Install OpenCover NuGet packageMore info and samples on: www.devarchweb.net
Implement test method that tests PersonManager.GetCalculatedPerson(int id) method and mocks database call with Fakes Imagine you need to write a codenamespace FakesBasics
{
public class Person
{
public int Id;
public string FirstName;
public string LastName;
public string FullName;
}
public interface IDataProvider
{
Person GetPerson(int id);
}
public class DataProvider : IDataProvider
{
public Person GetPerson(int id)
{
Person person = new Person();
// connect to database and populate static person data
throw new NotImplementedException();
// return person; unreachable code now, will be implemented in real world
}
}
public class PersonManager
{
IDataProvider _dataProvider;
public PersonManager(IDataProvider dataProvider)
{
_dataProvider = dataProvider;
}
public Person GetCalculatedPerson(int id)
{
// read form database
Person person = _dataProvider.GetPerson(id);
// calculate fields that change
person.FullName = person.FirstName + " " + person.LastName;
return person;
}
}
}
[TestMethod]
public void WhenGetCalucatedPersonIsCalled_ThenFullNameIsCalculated()
{
// arrange
int age = 26;
DateTime birthDay = DateTime.Now.AddYears(age);
IDataProvider fakeProvider = new FakesBasics.Fakes.StubIDataProvider()
{
GetPersonInt32 =
(id) => { return new Person() { Id = id, FirstName = "John", LastName = "First" }; }
};
PersonManager pm = new PersonManager(fakeProvider);
// act
Person p = pm.GetCalculatedPerson(1);
// assert
Assert.AreEqual("John First", p.FullName);
}
More info and samples on: www.devarchweb.net
Write a test that expects exception in NUnit NUnit - Assert.Throws[Test]
public void When_ExpectedExceptionIsThrownAndAssertThrowsIsUsed_Then_TestPasses()
{
NUnit.Framework.Assert.Throws(typeof(ArgumentException), delegate { MyProduct.MethodThatThrows(1); });
NUnit.Framework.Assert.Throws<ArgumentException>(delegate { MyProduct.MethodThatThrows(1); });
}
[Test]
// [NUnit.Framework.ExpectedException(typeof(ArgumentException))] Deprecated in NUnit 3
public void When_ExpectedExceptionIsThrownAndAttributeIsUsed_Then_TestPasses()
{
MyProduct.MethodThatThrows(1);
}
More info and samples on: www.devarchweb.net
Write a test method that tests various test conditions (for one method) in NUnit When using [TestCase] instead of [Test], NUnit will run 3 tests for the example below[TestCase("abc", 1)]
[TestCase("abc", 2)]
[TestCase("", 1)]
public void DemoTestKace(string arg1, int arg2)
{
// test code - arg1 and arg2 are populate with the values in [TestCase(..)]
}