Skip to content

Commit

Permalink
improve tests speed by reducing setup
Browse files Browse the repository at this point in the history
improved test speed by ~35% on my machine
  • Loading branch information
Dalet committed Mar 5, 2023
1 parent e76076c commit 49ca66d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 42 deletions.
18 changes: 7 additions & 11 deletions LeaderboardBackend.Test/Bans.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,11 @@ internal class Bans
private static User s_normalUser = null!;

[OneTimeSetUp]
public void OneTimeSetup()
public async Task OneTimeSetup()
{
s_factory = new();
s_apiClient = s_factory.CreateTestApiClient();
}

[OneTimeTearDown]
public void OneTimeTeardown()
{
s_factory.Dispose();
}

[SetUp]
public async Task SetUp()
{
s_factory.ResetDatabase();

s_adminJwt = (await s_apiClient.LoginAdminUser()).Token;
Expand Down Expand Up @@ -74,6 +64,12 @@ await s_apiClient.Post<Modship>(
s_modJwt = (await s_apiClient.LoginUser("[email protected]", "Passw0rd!")).Token;
}

[OneTimeTearDown]
public void OneTimeTeardown()
{
s_factory.Dispose();
}

[Test]
public static async Task CreateSiteBan_Ok()
{
Expand Down
12 changes: 4 additions & 8 deletions LeaderboardBackend.Test/Categories.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ internal class Categories
private static string? s_jwt;

[OneTimeSetUp]
public void OneTimeSetUp()
public async Task OneTimeSetUp()
{
s_factory = new TestApiFactory();
s_apiClient = s_factory.CreateTestApiClient();

s_factory.ResetDatabase();
s_jwt = (await s_apiClient.LoginAdminUser()).Token;
}

[OneTimeTearDown]
Expand All @@ -29,13 +32,6 @@ public void OneTimeTearDown()
s_factory.Dispose();
}

[SetUp]
public async Task SetUp()
{
s_factory.ResetDatabase();
s_jwt = (await s_apiClient.LoginAdminUser()).Token;
}

[Test]
public static void GetCategory_Unauthorized()
{
Expand Down
18 changes: 7 additions & 11 deletions LeaderboardBackend.Test/Judgements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,11 @@ internal class Judgements
private const string VALID_EMAIL = "[email protected]";

[OneTimeSetUp]
public void OneTimeSetup()
public async Task OneTimeSetup()
{
s_factory = new TestApiFactory();
s_apiClient = s_factory.CreateTestApiClient();
}

[OneTimeTearDown]
public void OneTimeTearDown()
{
s_factory.Dispose();
}

[SetUp]
public async Task SetUp()
{
s_factory.ResetDatabase();

// Set up a default Leaderboard and a mod user for that leaderboard to use as the Jwt for
Expand Down Expand Up @@ -89,6 +79,12 @@ public async Task SetUp()
s_jwt = (await s_apiClient.LoginUser(VALID_EMAIL, VALID_PASSWORD)).Token;
}

[OneTimeTearDown]
public void OneTimeTearDown()
{
s_factory.Dispose();
}

[Test]
public async Task CreateJudgement_OK()
{
Expand Down
14 changes: 5 additions & 9 deletions LeaderboardBackend.Test/Leaderboards.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ internal class Leaderboards
private static string? s_jwt;

[OneTimeSetUp]
public void OneTimeSetUp()
public async Task OneTimeSetUp()
{
s_factory = new TestApiFactory();
s_apiClient = s_factory.CreateTestApiClient();

s_factory.ResetDatabase();
s_jwt = (await s_apiClient.LoginAdminUser()).Token;
}

[OneTimeTearDown]
Expand All @@ -32,18 +35,11 @@ public void OneTimeTearDown()
s_factory.Dispose();
}

[SetUp]
public async Task SetUp()
{
s_factory.ResetDatabase();
s_jwt = (await s_apiClient.LoginAdminUser()).Token;
}

[Test]
public static void GetLeaderboard_NotFound()
{
RequestFailureException e = Assert.ThrowsAsync<RequestFailureException>(async () =>
await s_apiClient.Get<Leaderboard>($"/api/leaderboards/2", new()))!;
await s_apiClient.Get<Leaderboard>($"/api/leaderboards/{long.MaxValue}", new()))!;

Assert.AreEqual(HttpStatusCode.NotFound, e.Response.StatusCode);
}
Expand Down
7 changes: 4 additions & 3 deletions LeaderboardBackend.Test/Modships.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ internal class Modships
private static string s_jwt = null!;

[OneTimeSetUp]
public void OneTimeSetUp()
public async Task OneTimeSetUp()
{
s_factory = new TestApiFactory();
s_apiClient = s_factory.CreateTestApiClient();

s_jwt = (await s_apiClient.LoginAdminUser()).Token;
}

[OneTimeTearDown]
Expand All @@ -32,10 +34,9 @@ public void OneTimeTearDown()
}

[SetUp]
public async Task SetUp()
public void SetUp()
{
s_factory.ResetDatabase();
s_jwt = (await s_apiClient.LoginAdminUser()).Token;
}

[Test]
Expand Down

0 comments on commit 49ca66d

Please sign in to comment.