Add tests.

This commit is contained in:
xuef
2023-11-25 09:07:01 +00:00
parent 113ce326ab
commit 4aa237d1ac
3 changed files with 82 additions and 0 deletions
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>Aiursoft.ChessServer.Tests</RootNamespace>
<IsTestProject>true</IsTestProject>
<IsPackable>false</IsPackable>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="JunitXml.TestLogger" Version="3.0.134" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<PackageReference Include="AngleSharp" Version="1.0.7" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Aiursoft.ChessServer\Aiursoft.ChessServer.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,45 @@
using Aiursoft.CSTools.Tools;
using Microsoft.Extensions.Hosting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using static Aiursoft.WebTools.Extends;
namespace Aiursoft.ChessServer.Tests;
[TestClass]
public class BasicTests
{
private readonly string _endpointUrl;
private readonly int _port;
private readonly HttpClient _http;
private IHost? _server;
public BasicTests()
{
_port = Network.GetAvailablePort();
_endpointUrl = $"http://localhost:{_port}";
_http = new HttpClient();
}
[TestInitialize]
public async Task CreateServer()
{
_server = App<Startup>(Array.Empty<string>(), port: _port);
await _server.StartAsync();
}
[TestCleanup]
public async Task CleanServer()
{
if (_server == null) return;
await _server.StopAsync();
_server.Dispose();
}
[TestMethod]
[DataRow("/")]
public async Task GetHome(string url)
{
var response = await _http.GetAsync(_endpointUrl + url);
response.EnsureSuccessStatusCode(); // Status Code 200-299
}
}