-
Notifications
You must be signed in to change notification settings - Fork 1
Home
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.
NuGet: https://www.nuget.org/packages/Fonlow.Testing.Service
Launch IIS Express.
Test case: IntegrationTests.WCFTests.TestGetData
Example config:
<add key="Testing_UseIisExpress" value="True" />
<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/" />
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\
.
Mainly for launching IIS Express only once for one or multiple test classes in the same assembly that talk to the same Website.
Test cases: IntegrationTests.IisExpressFistureTests
NuGet: https://www.nuget.org/packages/Fonlow.Testing
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);
A base class for creating fixtures that could setup and tear down. The derived class is to be used with XUnit.ICollectionFixture.
NuGet: https://www.nuget.org/packages/Fonlow.Testing.Http
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.
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.
}
[Collection(TestConstants.IisExpressAndInit)]
public class ValuesTests : IClassFixture<DefaultHttpClientFixture>
{
public ValuesTests(DefaultHttpClientFixture restfulFixture)
{
baseUri = restfulFixture.BaseUri;
accessToken = restfulFixture.AccessToken;
authorizedClient = restfulFixture.AuthorizedClient;
api = new Values(authorizedClient, baseUri);
}
Uri baseUri;
string accessToken;
HttpClient authorizedClient;
Values api;
[Fact]
public void TestValuesGet()
{
var array = api.Get().ToArray();
Assert.Equal("value2", array[1]);
}
[Fact]
public void TestValuesGetId()
{
var r= api.Get(1, "something to say");
Assert.Equal("something to say1", r);
}
[Fact]
public void TestValuesGetId2()
{
var task = authorizedClient.GetStringAsync(new Uri(baseUri, "api/Values?id=1&name=value"));
var text = task.Result;
var jObject = JValue.Parse(text);
Assert.Equal("value1", jObject.ToObject<string>());
}
[Fact]
public void TestValuesPost()
{
var t = api.Post("value");
Assert.Equal("VALUE", t);
}
[Fact]
public void TestValuesPut()
{
var t = authorizedClient.PutAsync(new Uri(baseUri, "api/Values/1"), new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("", "value") }));
var ok = t.Result.IsSuccessStatusCode;
Assert.True(ok);
}