C# Unit Test Data Generators & Helpers

ParkSquare.Testing.Helpers

BUILD STATUS Continuous Integration Build Status

INSTALLATION

Install-Package ParkSquare.Testing.Helpers

DESCRIPTION

Collection of data generator classes built primarily to assist in unit testing and automated testing.

Documentation

This is a useful collection of generator classes built primarily to assist in unit testing. As Mark Seemann wrote, it's all too easy to hardcode set values in your tests. That said, you still need your tests to be predictable. One solution is to use a constrained set of input values instead, which means it is virtually impossible to hard-code any assertions in the subject under test. He calls this 'constrained non-determinism', and we use it a lot.

That's where these static generator classes come in, but can be used in any situation where you need valid or real-world test data.

Generators include:

  • Random numbers (int, byte, short, currency)
  • True/false coin toss
  • Dates & times, including fluent helpers for relative dates/times
  • Strings and chars
  • Credit card numbers
  • Debit card numbers
  • Bank account numbers
  • Bank sort codes (UK)
  • LUHN check passing data of any length
  • E-mail addresses
  • Names (male, female, child)
  • Enumeration selectors
  • List and unique lists of any object
  • IP Addresses (IP6 coming soon!)
  • Telephone numbers that pass validation routines (UK landline, UK mobile)
  • National Insurance (NI) numbers (UK)
  • UK Postcodes that pass validation routines
  • ...plus many more...

The library is built targeting .NET Standard 2.0 so can be used by your .NET Framework or .NET Core projects.


	// AccountNumberGenerator

	var accountNumber = AccountNumberGenerator.AnyValidAccountNumber();
	Console.WriteLine(accountNumber);

	// ArrayExtensions

	var testArry = new[] {"One", "Two", "Three"};
	var randomItem = testArry.AnyElement();
	Console.WriteLine(randomItem);

	// BooleanGenerator

	Console.WriteLine(BooleanGenerator.IsCoinTossHeads());

	// ByteGenerator

	Console.WriteLine(ByteGenerator.AnyByte());

	foreach (var b in ByteGenerator.AnyByteArrayOfSize(10))
	{
		Console.WriteLine(b);
	}

	// DoubleGenerator

	Console.WriteLine(DoubleGenerator.AnyDouble());
	Console.WriteLine(DoubleGenerator.AnyPositiveDouble());
	Console.WriteLine(DoubleGenerator.AnyNegativeDouble());

	// CharGenerator

	Console.WriteLine(CharGenerator.RandomAlpha());
	Console.WriteLine(CharGenerator.RandomAlphaNumeric());
	Console.WriteLine(CharGenerator.RandomChar());
	Console.WriteLine(CharGenerator.RandomDigit());

	// CreditCardGenerator

	Console.WriteLine(CreditCardGenerator.AnyCardNumber());
	Console.WriteLine(CreditCardGenerator.AnyCreditCardNumber());
	Console.WriteLine(CreditCardGenerator.AnyDebitCardNumber());
	Console.WriteLine(CreditCardGenerator.AnyMastercardCreditCardNumber());
	Console.WriteLine(CreditCardGenerator.AnyMastercardDebitCardNumber());
	Console.WriteLine(CreditCardGenerator.AnyVisaCreditCardNumber());
	Console.WriteLine(CreditCardGenerator.AnyVisaDebitCardNumber());

	// DateTimeExtensions

	Console.WriteLine(DateTime.Now.AnyTime());
	Console.WriteLine(DateTime.Now.AnyDifferentTime());
	Console.WriteLine(DateTime.Now.AnyTimeOtherThan(new TimeSpan(1, 2, 3, 4)));

	// DateTimeGenerator
	// NB: Date methods will have 00:00:00 as time portion, DateTime methods will have random time portion.

	Console.WriteLine(DateTimeGenerator.AnyDate());
	Console.WriteLine(DateTimeGenerator.AnyTime());
	Console.WriteLine(DateTimeGenerator.AnyDateAfter(DateTime.Now));
	Console.WriteLine(DateTimeGenerator.AnyDateBefore(DateTime.Now));
	Console.WriteLine(DateTimeGenerator.AnyDateBetween(DateTime.Now, new DateTime(2099, 1, 1)));
	Console.WriteLine(DateTimeGenerator.AnyDateExcept(DateTime.Now));
	Console.WriteLine(DateTimeGenerator.AnyDateTime());
	Console.WriteLine(DateTimeGenerator.AnyDateTimeAfter(DateTime.Now));
	Console.WriteLine(DateTimeGenerator.AnyDateTimeExcept(DateTime.Now));

	var pair1 = DateTimeGenerator.AnyPairOfDateTimes();
	Console.WriteLine(pair1.Min + ", " + pair1.Max);

	var pair2 = DateTimeGenerator.AnyPairOfDates();
	Console.WriteLine(pair2.Min + ", " + pair2.Max);

	var relative1 = DateTimeGenerator.AnyRelativeDateTimes();
	Console.WriteLine("Now: " + relative1.Now + " Past: " + relative1.InThePast + " Future: " + relative1.InTheFuture);

	Console.WriteLine(DateTimeGenerator.FirstDayOfThisMonth());
	Console.WriteLine(DateTimeGenerator.FirstDayOfMonth(DateTime.Now));
	Console.WriteLine(DateTimeGenerator.LastDayOfThisMonth());
	Console.WriteLine(DateTimeGenerator.LastDayOfMonth(DateTime.Now));
	Console.WriteLine(DateTimeGenerator.Today());
	Console.WriteLine(DateTimeGenerator.Tomorrow());

	// DecimalExtensions

	var d1 = 1.23M;
	Console.WriteLine(d1.LimitedBy(1, 2));
	Console.WriteLine(d1.ReverseSign());
	Console.WriteLine(d1.Absolute());

	// DecimalGenerator

	Console.WriteLine(DecimalGenerator.AnyCurrencyAmount());
	Console.WriteLine(DecimalGenerator.AnyCurrencyAmountGreaterThan(100));
	Console.WriteLine(DecimalGenerator.AnyDecimalInHalfOpenRange(10, 100));
	Console.WriteLine(DecimalGenerator.AnyPositiveCurrencyAmount());

	// EmailAddressGenerator

	Console.WriteLine(EmailAddressGenerator.AnyEmailAddress());

	// EnumerableExtensions

	var e1 = new[] {"one", "two", "three", "four", "five"};
	var e2 = e1.Shuffle();
	foreach (var e in e2) Console.WriteLine(e);

	Console.WriteLine(e1.AnyItem());

	e1.ForEach(Console.WriteLine);

	// EnumerableGenerator

	var e3 = EnumerableGenerator.BoundedSequence(10, CreateNewThing);
	e3.ForEach(Console.WriteLine);

	var e4 = EnumerableGenerator.UniqueBoundedSequence(10, CreateNewThing);
	e4.ForEach(Console.WriteLine);

	// EnumGenerator

	Console.WriteLine(EnumGenerator.AnyEnumValue<ThingEnumeration>());
	Console.WriteLine(EnumGenerator.AnyEnumValueExcept(ThingEnumeration.Hello));
	Console.WriteLine(EnumGenerator.AnyEnumValueIn(ThingEnumeration.Hello, ThingEnumeration.World));

	// IntegerGenerator

	Console.WriteLine(IntegerGenerator.AnyInteger());
	Console.WriteLine(IntegerGenerator.AnyIntegerInHalfOpenRange(100, 1000));

	var i1 = IntegerGenerator.AnyIntegerListOfSize(10);
	i1.ForEach(Console.WriteLine);

	var i2 = IntegerGenerator.UniquePositiveIntegerListOfSize(10);
	i2.ForEach(Console.WriteLine);

	// IpAddressGenerator

	Console.WriteLine(IpAddressGenerator.AnyIpAddress());
	IpAddressGenerator.GenerateRandomNonRepeatingIpAddresses(10).ForEach(Console.WriteLine);

	// LuhnGenerator

	Console.WriteLine(LuhnGenerator.AnyValidNumber(10));
	Console.WriteLine(LuhnGenerator.CalculateCheckDigit("12345"));


	// NameGenerator

	Console.WriteLine(NameGenerator.AnyName());
	Console.WriteLine(NameGenerator.AnyAdultTitle());
	Console.WriteLine(NameGenerator.AnyChildTitle());
	Console.WriteLine(NameGenerator.AnyFemaleForename());
	Console.WriteLine(NameGenerator.AnyMaleForename());
	Console.WriteLine(NameGenerator.AnyForename());
	Console.WriteLine(NameGenerator.AnyMaleName());
	Console.WriteLine(NameGenerator.AnyFemaleName());
	Console.WriteLine(NameGenerator.AnySurname());
	Console.WriteLine(NameGenerator.AnyTitle());

	// NationalInsuranceNumberGenerator

	Console.WriteLine(NationalInsuranceNumberGenerator.Any());
	Console.WriteLine(NationalInsuranceNumberGenerator.AnyFormatted());

	// PostCodeGenerator

	Console.WriteLine(PostcodeGenerator.AnyPostcode());
	Console.WriteLine(PostcodeGenerator.AnyValidPostcode());

	// ShortGenerator

	Console.WriteLine(ShortGenerator.AnyShort());
	Console.WriteLine(ShortGenerator.AnyShortInHalfOpenRange(10, 100));

	// SortCodeGenerator

	Console.WriteLine(SortCodeGenerator.AnyValidSortCode());
	Console.WriteLine(SortCodeGenerator.AnyValidSortCodeWithDashes());

	// StringGenerator

	Console.WriteLine(StringGenerator.AnyNonNullString());
	Console.WriteLine(StringGenerator.AnyEmptyOrWhitespaceString());
	Console.WriteLine(StringGenerator.AnyStringOfSizeAndCase(100, CaseType.TitleCase));
	Console.WriteLine(StringGenerator.SequenceOfAlphaNumerics(10));
	Console.WriteLine(StringGenerator.SequenceOfAlphas(10));
	Console.WriteLine(StringGenerator.SequenceOfDigits(10));

	// TelephoneNumberGenerator

	Console.WriteLine(TelephoneNumberGenerator.AnyLandlineNumber());
	Console.WriteLine(TelephoneNumberGenerator.AnyMobileNumber());
	Console.WriteLine(TelephoneNumberGenerator.AnyTelephoneNumber());