-
Notifications
You must be signed in to change notification settings - Fork 4
/
TestsWithReports.cs
92 lines (76 loc) · 3.32 KB
/
TestsWithReports.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections.Generic;
using System.Net;
using AventStack.ExtentReports;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using NUnit.Framework.Constraints;
using RestSharp;
using RestSharp.Authenticators;
using RestSharp.Serialization.Json;
using RestSharpDemo.Utilities;
namespace RestSharpDemo.Tests
{
[TestFixture]
public class TestsWithReports
{
private IRestClient _restClient;
private IRestRequest _restRequest;
private IRestResponse _restResponse;
private const string BaseUrl = "https://reqres.in/";
[OneTimeSetUp]
public void SetupReport()
{
var dir = TestContext.CurrentContext.TestDirectory;
//var dir = TestContext.CurrentContext.TestDirectory; Or give a path where you'd want your test reports.
Console.WriteLine(dir);
Reporter.SetupExtentReport("RestSharp Demo Framework", "Extent Reports for API tests", dir);
}
[SetUp]
public void Setup()
{
Reporter.CreateTest(TestContext.CurrentContext.Test.Name);
_restClient = new RestClient(BaseUrl);
}
[Test]
public void Test_01_SimpleGetRequestTest()
{
//Arrange
_restRequest = new RestRequest("api/users/{user}", Method.GET);
_restRequest.AddUrlSegment("user", 2);
//Act
_restResponse = _restClient.Execute(_restRequest);
//Assert
Console.WriteLine("**** This is the response **** "+ _restResponse.Content);
Assert.That(_restResponse.StatusCode, Is.EqualTo(HttpStatusCode.OK));
}
[Test]
public void Test_02_VerifyTotalNumberOfRecords()
{
_restRequest= new RestRequest("/api/users?page=2", Method.GET);
_restResponse = _restClient.Execute(_restRequest);
var result = _restResponse.DeserializeResponse()["total"];
Assert.That(result, Is.EqualTo("12"), "Total Records are mismatch");
}
[Test]
public void Test_03_GetRequestWithAuthenticationParams()
{
_restClient.Authenticator = new HttpBasicAuthenticator("[email protected]", "cityslicka");
_restRequest = new RestRequest("https://reqres.in/api/login", Method.GET);
_restResponse = _restClient.Execute(_restRequest);
Assert.That(_restResponse.StatusCode, Is.EqualTo(HttpStatusCode.OK));
}
[TearDown]
public void Close()
{
Console.WriteLine("Hey! I've done executing all the tests");
var testStatus = TestContext.CurrentContext.Result.Outcome;
Reporter.LogReport(Status.Info, "Test ended with status: " + testStatus.ToString());
}
[OneTimeTearDown]
public void CloseForReport()
{
Reporter.FlushReport();
}
}
}