You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<SectionHeading="What are integration tests in context of APIs?"Level="4">
14
+
<Section>
15
+
<p>This is going to be multi-part series on integration tests from introduction to advanced used cases.</p>
16
+
</Section>
17
+
18
+
<SectionHeading="What are integration tests in context of APIs?">
10
19
<p>In the context of <b>.NET Core APIs</b> for a blog, integration tests are automated tests that evaluate the
11
20
functionality of various parts of your application working together as a whole. Specifically, these tests
12
21
ensure that multiple components — such as controllers, database access, middleware, internal and external
@@ -28,7 +37,7 @@
28
37
29
38
</Section>
30
39
31
-
<SectionHeading="Ways to write integration tests"Level="4">
40
+
<SectionHeading="Ways to write integration tests">
32
41
<p>Now there are various ways to write integration tests:</p>
33
42
<ul>
34
43
<li>Using Postman or any other API testing tool</li>
@@ -48,7 +57,7 @@
48
57
</ul>
49
58
</Section>
50
59
51
-
<SectionHeading="Diving into the code"Level="4">
60
+
<SectionHeading="Diving into the code">
52
61
<p>For a demo code, we will be referring a code repository, which stores the information for superhero personalities. I’ll be sharing the GitHub link of the code repo at the end of the article.</p>
53
62
<p>Our demo API has 2 APIs:</p>
54
63
@@ -84,7 +93,7 @@ public class SuperHeroController(ISuperHeroRepository superHeroRepository)
84
93
<p>Since this is basic demo, our ISuperHeroRepository interacts with a Sqlite database which has some predefined SuperHero entries.</p>
85
94
</Section>
86
95
87
-
<SectionHeading="Integration test example"Level="4">
96
+
<SectionHeading="Integration test example">
88
97
<p>Here is an example for our integration test for <b>Get SuperHero By Id</b> scenario:</p>
89
98
90
99
<CodeSnippetDescription='Integration test for Get SuperHero By Id'Number="2">
@@ -140,13 +149,13 @@ public async Task Get_ById_SuperHero_Returns_SuperHero()
140
149
<h5>4. Assert: Validating the Response</h5>
141
150
<p>Now we enter the Assert phase, where we check if the response from the API matches our expectations.</p>
142
151
143
-
<p><b>Checking the Status Code:</b> This ensures that the API returns the expected HTTP status code, confirming the success or failure of the request.</p>
152
+
<p><b>Checking the Status Code:</b></p>
144
153
<CodeSnippetDescription='Assert Status Code'Number="6">
<p>This assertion ensures the API returns a status code of <code>200 OK</code>, confirming that the request was successful. If the API returned any other status code (like <code>404 Not Found</code> or <code>500 Internal Server Error</code>), the test would fail.</p>
148
157
</Section>
149
-
<SectionHeading="Validating the Returned Data"Level="4">
158
+
<SectionHeading="Validating the Returned Data">
150
159
<p>Next, we check the actual content of the response by deserializing the JSON response into a <code>SuperHero</code> object:</p>
151
160
152
161
<CodeSnippetDescription='Validate Returned Data'Number="7">
@@ -160,7 +169,7 @@ public async Task Get_ById_SuperHero_Returns_SuperHero()
160
169
</ul>
161
170
</Section>
162
171
163
-
<SectionHeading="Verifying Specific Property Values"Level="4">
172
+
<SectionHeading="Verifying Specific Property Values">
164
173
<p>Finally, we verify that the returned superhero has the correct properties, specifically checking that the ID is 1 and the superhero's name is "Batman":</p>
165
174
166
175
<CodeSnippetDescription='Verify Specific Property Values'Number="8">
@@ -173,7 +182,7 @@ public async Task Get_ById_SuperHero_Returns_SuperHero()
173
182
<BlogImageImagePath="/images/blog/integration-testing/intro/Success test result.webp"Description="Success test result"Number="4" />
174
183
</Section>
175
184
176
-
<SectionHeading="Negative Scenario"Level="4">
185
+
<SectionHeading="Negative Scenario">
177
186
<p>Let’s deliberately try to break our code to understand how the test behaves in a negative scenario. The test scenario <b>Get superhero by invalid Id returns not found</b> expects a <code>404 Not Found</code> status code when a superhero is not found.</p>
178
187
179
188
<p>We will make a change in our code to return <code>400 Bad Request</code> instead of <code>404 Not Found</code>:</p>
@@ -196,7 +205,7 @@ public async Task Get_ById_SuperHero_Returns_SuperHero()
Expected response.StatusCode to be HttpStatusCode.NotFound {value:404},
199
-
but found HttpStatusCode.BadRequest {value:400}
208
+
but found HttpStatusCode.BadRequest {value:400}.
200
209
</CodeSnippet>
201
210
202
211
<BlogImageImagePath="/images/blog/integration-testing/intro/Failure result status.webp"Description="Failure result status"Number="5" />
@@ -206,6 +215,6 @@ public async Task Get_ById_SuperHero_Returns_SuperHero()
206
215
<p>Such scenarios highlight the importance of integration tests in catching unintended changes or bugs in the application behavior.</p>
207
216
</Section>
208
217
<p>This is it for the basic setup demo. I will be covering more in the future articles like, how to work with database, authentication, events etc.</p>
0 commit comments