Training
Module
C# testing in Visual Studio - Training
Start testing your C# apps by using the testing tools in Visual Studio. Learn to write tests, use Test Explorer, create test suites, and apply the red, green, refactor pattern to write code.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Applies To:# OData WebApi v7 for aspnet webapi supported OData AspNet WebApi V7# OData Webapi for Webapi supported OData AspNet WebApi V6
In OData WebApi, there are unit tests, e2e tests for V3 and V4, those test cases are to verify that features and bug fixes work as intended, and also to make sure they do not break existing functionality.
Every class in OData WebApi has its own unit test class, for example: OData/src/System.Web.OData/OData/Builder/ActionLinkBuilder.cs 's test class is OData/test/UnitTest/System.Web.OData.Test/OData/Builder/ActionLinkBuilderTests.cs
You can find that the structures under the System.Web.OData
folder and theSystem.Web.OData.Test
folder are the same, and the same goes for V3 System.Web.Http.OData.Test
. So if your pull request contains any class addition/change, you should add/change (here "change" means adding test cases) the corresponding unit test file.
E2E test are complete test for user scenarios, always begin with client request and end with server response. If your unit test in pull request can't cover all scenario well or you have a big pull request, please add E2E test for it.
[NuwaFramework]
public class MyTest
{
[NuwaBaseAddress]
public string BaseAddress { get; set; }
[NuwaHttpClient]
public HttpClient Client { get; set; }
[NuwaConfiguration]
public static void UpdateConfiguration(HttpConfiguration config)
{
config.Routes.MapODataRoute("odata", "odata", GetModel());
}
private static IEdmModel GetModel()
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
var customers = builder.EntitySet<Customer>("Customers");
var orders = builder.EntitySet<Order>("Orders");
return builder.GetEdmModel();
}
[Fact]
public void GetCustomersWork()
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get,BaseAddress + "/odata/Customers");
HttpResponseMessage response = Client.SendAsync(request).Result;
Assert.Equal(HttpStatusCode.OK,response.StatusCode);
}
}
[NuwaFramework]
public class MyTest2
{
[NuwaBaseAddress]
public string BaseAddress { get; set; }
[NuwaHttpClient]
public HttpClient Client { get; set; }
[NuwaConfiguration]
public static void UpdateConfiguration(HttpConfiguration config)
{
config.Routes.MapODataRoute("odata", "odata", GetModel());
}
private static IEdmModel GetModel()
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
var customers = builder.EntitySet<Customer>("Customers");
var orders = builder.EntitySet<Order>("Orders");
return builder.GetEdmModel();
}
[Fact]
public void GetCustomersWork()
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, BaseAddress + "/odata/Customers");
HttpResponseMessage response = Client.SendAsync(request).Result;
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
}
public class CustomersController : ODataController
{
[Queryable(PageSize = 3)]
public IHttpActionResult Get()
{
return Ok(Enumerable.Range(0, 10).Select(i => new Customer
{
Id = i,
Name = "Name " + i
}));
}
public IHttpActionResult Post(Customer customer)
{
return Created(customer);
}
}
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public IList<Order> Orders { get; set; }
}
public class Order
{
public int Id { get; set; }
public DateTime PurchaseDate { get; set; }
}
Training
Module
C# testing in Visual Studio - Training
Start testing your C# apps by using the testing tools in Visual Studio. Learn to write tests, use Test Explorer, create test suites, and apply the red, green, refactor pattern to write code.