Jesús Estévez
Just another simple developer
@javigm
@jecaestevez
@javigm
@jecaestevez
@javigm
@jecaestevez
Boring introduction
Take long time during the long term
Require complex configuration
Limit visibility, difficult to do Regression Tests
Flexible
Executed by tools and computer, it’s significantly faster than manual.
Regression Testing easy with automation tools.
Find more bugs than a human tester
Better visibility into app performance
vs
@javigm
Boring introduction
@javigm
Boring introduction
@jecaestevez
Boring introduction
@javigm
Automated and Repeatable.
Easy to implement and run quickly.
Fully isolated (runs independently of the other tests)
Readable and in case of fail must be describable and understandable
AAA (Arrange / Act / Assert)
Boring introduction
@javigm
Importance to write good unit tests
Choose good testing framework (Nunit or Xunit)
Ignore Test we are not secure (not adding comments)
One Assert Per Test Method (Assert with message)
Limit Use of Mocks
Don't Mock Entity Framework completely
Clean Code (SOLID, design patterns, DRY)
@jecaestevez
Testing Tips
@jecaestevez
[Authorize]
public class LocationController : ApiController
{
// PUT api/location/5
public LocationBase Put(int id, [FromBody]LocationBase value)
{
}
}
[TestClass]
public class LocationControllerTest
{
LocationController locController;
[TestInitialize]
public void Setup()
{
locController = new LocationController();
}
}
[TestClass]
public class LocationControllerTest
{
LocationController locController;
[TestInitialize]
public void Setup()
{
locController = new LocationController();
}
[TestMethod]
public void PutNewLocation_v1()
{
LocationBase newLocation = new LocationBase()
{ Id = 0, DisplayName = "Los Motriles", Name = "Motril", Latitude = 36, Longitude = -3 };
var locationSaved = locController.Put(0, newLocation);
Assert.IsTrue(locationSaved.Id > 0);
Assert.AreEqual(locationSaved.DisplayName, newLocation.DisplayName);
Assert.AreEqual(locationSaved.Name, newLocation.Name);
Assert.AreEqual(locationSaved.Latitude, newLocation.Latitude);
Assert.AreEqual(locationSaved.Longitude, newLocation.Longitude);
}
}
Testing Tips
@jecaestevez
[Authorize]
public class LocationController : ApiController
{
// PUT api/location/5
public LocationBase Put(int id, [FromBody]LocationBase value)
{
}
}
[TestClass]
public class LocationControllerTest
{
........
[TestMethod]
public void PutNewLocation_v1()
{
LocationBase newLocation = new LocationBase()
{ Id = 0, DisplayName = "Los Motriles", Name = "Motril", Latitude = 36, Longitude = -3 };
var locationSaved = locController.Put(0, newLocation);
Assert.IsTrue(locationSaved.Id > 0);
Assert.AreEqual(locationSaved.DisplayName, newLocation.DisplayName);
Assert.AreEqual(locationSaved.Name, newLocation.Name);
Assert.AreEqual(locationSaved.Latitude, newLocation.Latitude);
Assert.AreEqual(locationSaved.Longitude, newLocation.Longitude);
}
}
[TestClass]
public class LocationControllerTest
{
........
[TestMethod]
public void PutNewLocation_GeneratingAssets()
{
//First generate the json asset for example
// new JavaScriptSerializer().Serialize(newLocation);
var locJson = TestUtil.ReadJsonFile("Motril_Location.json", "..\\Assets");
LocationBase newLocation = JsonConvert.DeserializeObject<LocationBase>(locJson);
var locationSaved = locController.Put(0, newLocation);
Assert.IsTrue(locationSaved.Id > 0);
Assert.AreEqual(locationSaved.DisplayName, newLocation.DisplayName);
Assert.AreEqual(locationSaved.Name, newLocation.Name);
Assert.AreEqual(locationSaved.Latitude, newLocation.Latitude);
Assert.AreEqual(locationSaved.Longitude, newLocation.Longitude);
}
}
Testing Tips
@jecaestevez
[TestClass]
public class LocationControllerTest
{
........
[TestMethod]
public void PutNewLocation_GeneratingAssets()
{
//First generate the json asset for example
// new JavaScriptSerializer().Serialize(newLocation);
var locJson = TestUtil.ReadJsonFile("Motril_Location.json", "..\\Assets");
LocationBase newLocation = JsonConvert.DeserializeObject<LocationBase>(locJson);
var locationSaved = locController.Put(0, newLocation);
Assert.IsTrue(locationSaved.Id > 0);
Assert.AreEqual(locationSaved.DisplayName, newLocation.DisplayName);
Assert.AreEqual(locationSaved.Name, newLocation.Name);
Assert.AreEqual(locationSaved.Latitude, newLocation.Latitude);
Assert.AreEqual(locationSaved.Longitude, newLocation.Longitude);
}
}
[TestClass]
public class LocationControllerTest
{
........
[TestMethod]
public void PutNewLocation_SmartAsserts()
{
LocationBase newLocation = ArrageLocation("Motril_Location.json", "..\\Assets");
LocationBase resultLocation = ArrageLocation("Motril_GeoLocated.json", "..\\Assets");
var locationSaved = locController.Put(0, newLocation);
Assert.AreEqual(locationSaved, resultLocation);
}
private LocationBase ArrageLocation(string fileName,string path)
{
var locJson = TestUtil.ReadJsonFile(fileName, path);
return JsonConvert.DeserializeObject<LocationBase>(locJson);
}
}
Testing Tips
@jecaestevez
[Authorize]
public class LocationController : ApiController
{
// POST api/Account/Copy
[Route("Copy")]
public IHttpActionResult Copy(int sourceLocationId)
{
return Ok();
}
}
[TestClass]
public class LocationControllerTest
{
........
[TestMethod]
public void PutNewLocation_SmartAsserts()
{
//Arrange
LocationBase newLocation = ArrageLocation("Motril_Location.json", "..\\Assets");
var locationSaved = locController.Put(0, newLocation);
//Act
var locationCopied = locController.Copy(locationSaved.Id);
//Asert
Assert.IsTrue(ExecuteSqlStoreProcedure("assert_Copy",locationSaved.Id, locationSaved.Id));
}
private bool ExecuteSqlStoreProcedure(string spName,int sourceId , int destinationId)
{
...........
}
}
@javigm
Advanced Techniques of Testing
Moq
FluentAssertions
AutoFixture
Integration Tests
Project: AdventureWorks2017 Api:
Swagger, Ninject, AutoMapper, WebActivatorEx, EF6
@javigm
@javigm
Advanced Techniques of Testing
@javigm
Advanced Techniques of Testing
@javigm
Advanced Techniques of Testing
@javigm
Advanced Techniques of Testing
@javigm
@jecaestevez
@jecaestevez
@javigm
The Art Of Unit Testing
Clean Code:
A Handbook of Agile Software Craftsmanship
@javigm
@jecaestevez
By Jesús Estévez