Types of unit tests in BFC API
Every BFC API must be covered with unit tests. We tend to have two types of unit tests in our solution: ordinary unit tests and integration tests. Believe ordinary unit tests do not need an introduction while integration unit tests need some explanation. During our Build step we want to make sure that all API components are in place and that endpoints work as expected. .NET provides functionality to run API tests in the form of unit tests. We actively use it and make sure that every API endpoint is covered with at least one “happy path” integration unit test. For example this at test that we have for our /docs endpoint:
[TestInitialize]public void Initialize(){ Factory = new WebApplicationFactory<Startup>(); Client = Factory.CreateClient();}
[TestMethod]public async Task ReturnApiNameAndDocsUrl(){ // Arrange var apiName = Startup.ApiName; var docsUrl = "https://localhost/docs";
// Act var homeResponse = await Client!.GetAsync("/"); var homeResponseBody = await homeResponse.Content.ReadAsStringAsync(); homeResponse.EnsureSuccessStatusCode();
var homeResponseContent = JsonConvert.DeserializeObject<JObject>(homeResponseBody);
// Assert Assert.AreEqual(apiName, homeResponseContent?["message"]?.ToString()); Assert.AreEqual(docsUrl, homeResponseContent?["docs"]?.ToString());}