Skip to content
fonlow edited this page Jun 15, 2024 · 17 revisions

!!!This chapter is outdated with classes dated back to .NET Framework era, either being removed or being obsolete!!!

Please refer to the main README.md

This component set is for you to construct unit testing and integration testing with dependencies on external resources like services, databases and security etc.

The documentation here is minimal. And in the following chapters, if no detailed description is given, you are going to read the doc comments of classes and the cases of the unit testing or integration testing.

Assembly Fonlow.Testing.Service[Core]

Class IisExpressFixture for launching IIS Express and class DotNetHostFixture for launching DotNet host

Example app.config for .NET Framework test suites:

    <add key="Testing_HostSite" value="Fonlow.DemoService" />
    <add key="Testing_HostSiteApplicationPool" value="Clr4IntegratedAppPool" />
    <add key="Testing_SlnRoot" value="SLN_ROOT_.VS" />
    <add key="Testing_SlnName" value="YourSlnNameWithoutExtension" />
    <add key="Testing_BaseUrl" value="http://localhost:4060/" />

Example appsettings.json for .NET Core test suites launching IIS Express:

{
	"Testing": {
		"BaseUrl": "http://localhost:60921/",
		"HostSite": "DemoWebApi",
		"HostSiteApplicationPool": "Clr4IntegratedAppPool",
		"SlnName": "FonlowTesting",
		"SlnRoot": "SLN_ROOT_.VS"
	}
}

Example appsettings.json for .NET Core test suites launching DotNet Host:

{
	"Testing": {
		"BaseUrl": "http://localhost:5000/",
		"DotNetServiceAssemblyPath": "..\\..\\..\\..\\DemoCoreWeb\\bin\\Debug\\netcoreapp3.0\\DemoCoreWeb.dll"
	}
}

Hints:

In Visual Studio 2015 update 2 and above, the information of IIS Express used by IDE is stored in YourSolutionFolder\.vs\config\applicationhost.config, while in older versions the information is stored in %userprofile%\documents\iisexpress\config\applicationhost.config. So for older versions of VS, you don't need to define Testing_SlnRoot or leave the value empty. For VS 2015 update 2 or above, the value should generally be SLN_ROOT_.VS. For VS 2019, you also need Testing_SlnName, because applicationhost.config is stored at YourSolutionFolder\.vs\YourSlnNameWithoutExtension/config\.

Assembly Fonlow.Testing.Http[Core]

Wrap a HttpClient instance with a bearer token initialized through username and password. A HttpClient instance could be used concurrently to send multiple requests. Having a bearer token will make subsequent request fast without the need to login again and again.

Class DefaultHttpClient and class DefaultHttpClientWithUsername for HttpClient fixture

If all test cases in an assembly are using the same base URL and the same credential, you may use the fixture along with XUnit.ICollectionFixture.

Sample:

    public class TestConstants
    {
        public const string IisExpressAndInit = "IISExpressStartup";
    }

    /// <summary>
    /// Load IIS Express once for all test classes using the same Collection
    /// http://xunit.github.io/docs/shared-context.html
    /// </summary>
    [CollectionDefinition(TestConstants.IisExpressAndInit)]
    public class IisCollection : ICollectionFixture<Fonlow.Testing.IisExpressFixture>
    {
        // This class has no code, and is never created. Its purpose is simply
        // to be the place to apply [CollectionDefinition] and all the
        // ICollectionFixture<> interfaces.
    }

    public class EntitiesFixture : DefaultHttpClient
    {
        public EntitiesFixture()
        {
            Api = new DemoWebApi.Controllers.Client.Entities(HttpClient);
        }

        public DemoWebApi.Controllers.Client.Entities Api { get; private set; }
    }


    [Collection(TestConstants.IisExpressAndInit)]
    public class EntitiesApiIntegration : IClassFixture<EntitiesFixture>
    {
        public EntitiesApiIntegration(EntitiesFixture fixture)
        {
            api = fixture.Api;
        }

        DemoWebApi.Controllers.Client.Entities api;

        [Fact]
        public void TestCreatePerson()
        {
            Person person = new Person()
            {
                Name = "Some One",
                Surname = "One",
                GivenName = "Some",
                DOB = DateTime.Now.AddYears(-20),
                Addresses = new Address[]{new Address(){
                    City="Brisbane",
                    State="QLD",
                    Street1="Somewhere",
                    Street2="Over the rainbow",
                    PostalCode="4000",
                    Country="Australia",
                    Type= AddressType.Postal,
                    Location = new DemoWebApi.DemoData.Another.Client.MyPoint() {X=4, Y=9 },
                }},
            };

            var id = api.CreatePerson(person);
            Assert.True(id > 0);
        }

Assembly Fonlow.Testing

Class PermissionPrincipalFixture

For functions of a service class decorated by SecurityPermissionAttribute.

Service function sample:

        [PrincipalPermission(SecurityAction.Demand, Role = RoleConstants.Admin)]
        public void DeleteAccountByUserName(string userName)
        {
            RemoveAccountByUserName(userName);
        }

Test case sample:

            var service2 = new UserManagementImp();
            Fonlow.Testing.PermissionPrincipalFixture.SetPrincipal("Admin");
            service2.DeleteAccountByUserName(loginName);

Class SetupAndTearDownFixture

A base class for creating fixtures that could setup and tear down. The derived class is to be used with XUnit.ICollectionFixture.