From 49ca66dca44e91e8e0484fc1c2f71d1fe35336cc Mon Sep 17 00:00:00 2001 From: Dalet Date: Sun, 5 Mar 2023 23:05:51 +0900 Subject: [PATCH] improve tests speed by reducing setup improved test speed by ~35% on my machine --- LeaderboardBackend.Test/Bans.cs | 18 +++++++----------- LeaderboardBackend.Test/Categories.cs | 12 ++++-------- LeaderboardBackend.Test/Judgements.cs | 18 +++++++----------- LeaderboardBackend.Test/Leaderboards.cs | 14 +++++--------- LeaderboardBackend.Test/Modships.cs | 7 ++++--- 5 files changed, 27 insertions(+), 42 deletions(-) diff --git a/LeaderboardBackend.Test/Bans.cs b/LeaderboardBackend.Test/Bans.cs index 535d7b5e..0fd217e4 100644 --- a/LeaderboardBackend.Test/Bans.cs +++ b/LeaderboardBackend.Test/Bans.cs @@ -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; @@ -74,6 +64,12 @@ await s_apiClient.Post( s_modJwt = (await s_apiClient.LoginUser("mod@email.com", "Passw0rd!")).Token; } + [OneTimeTearDown] + public void OneTimeTeardown() + { + s_factory.Dispose(); + } + [Test] public static async Task CreateSiteBan_Ok() { diff --git a/LeaderboardBackend.Test/Categories.cs b/LeaderboardBackend.Test/Categories.cs index 87adbe30..cc5735fc 100644 --- a/LeaderboardBackend.Test/Categories.cs +++ b/LeaderboardBackend.Test/Categories.cs @@ -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] @@ -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() { diff --git a/LeaderboardBackend.Test/Judgements.cs b/LeaderboardBackend.Test/Judgements.cs index 47f0144c..0c2e881a 100644 --- a/LeaderboardBackend.Test/Judgements.cs +++ b/LeaderboardBackend.Test/Judgements.cs @@ -25,21 +25,11 @@ internal class Judgements private const string VALID_EMAIL = "test@email.com"; [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 @@ -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() { diff --git a/LeaderboardBackend.Test/Leaderboards.cs b/LeaderboardBackend.Test/Leaderboards.cs index e76b5976..7dd669c9 100644 --- a/LeaderboardBackend.Test/Leaderboards.cs +++ b/LeaderboardBackend.Test/Leaderboards.cs @@ -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] @@ -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(async () => - await s_apiClient.Get($"/api/leaderboards/2", new()))!; + await s_apiClient.Get($"/api/leaderboards/{long.MaxValue}", new()))!; Assert.AreEqual(HttpStatusCode.NotFound, e.Response.StatusCode); } diff --git a/LeaderboardBackend.Test/Modships.cs b/LeaderboardBackend.Test/Modships.cs index db5c2e93..4d7dff01 100644 --- a/LeaderboardBackend.Test/Modships.cs +++ b/LeaderboardBackend.Test/Modships.cs @@ -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] @@ -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]