Codemotion 2015 - Unit Testing

35
Unit Testing El mito de los cero bugs MADRID · NOV 27-28 · 2015

Transcript of Codemotion 2015 - Unit Testing

Page 1: Codemotion 2015 - Unit Testing

Unit Testing El mito de los cero bugsMADRID · NOV 27-28 · 2015

Page 2: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

Fernando [email protected]@tokiota.com

Page 3: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

Agenda Definición de Unit Test

Estructura Código Testeable Unit Test

EstructuraHerramientas

Conclusiones

Page 4: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

¿Qué es una prueba de software?

Input Process OutputCalidad

Page 5: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

Clasificación de las pruebasInstallation testingCompatibility testingSmoke and sanity testingRegression testingAcceptance testingAlpha testingBeta testingFunctional vs non-functional testingDestructive testingSoftware performance testingUsability testingAccessibility testingSecurity testingInternationalization and localizationDevelopment testingA/B testing

Unit testingIntegration testingSystem testingAcceptance testing

White-Box testingBlack-box testing Visual testingGrey-box testing

Page 6: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

¿Qué es una prueba unitaria?

Page 7: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

¿Qué es una prueba unitaria?A unit test is a piece of a code (usually a method) that invokes another piece of code and checks the correctness of some assumptions afterward. If the assumptions turn out to be wrong, the unit test has failed.

A “unit” is a method or function.Unit test definition – The art of unit testingRoy Osherove – Manning Publications co

Page 8: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

Caraterísticas FIRST Fast Isolated Repeatable Self-Validating Timely

Page 9: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

Caraterísticas SECOND Profesional Unitario Automatizable No usa recursos

Page 10: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

EstructuraTriple A Arrange Act Assert

Page 11: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

EstructuraTriple Cuádruple A Assume Arrange Act Assert

Page 12: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

Estructura

Page 13: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

Si un método o función es una unidadtengo que desglosarlos al máximo

Vs.

Page 14: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

Page 15: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

¿Puedo escribir mejores tests?

Page 16: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

Puedo escribir mejor código

Page 17: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

Desacoplar

Page 18: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

Page 19: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

Usar patrones de diseño Inversion Of Control Abstract Factory …

Page 20: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

Test Doubles Dummies Fakes Spies Stubs Mocks

Page 21: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

Page 22: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

[TestMethod]public async Task SpyTest(){ // arange var service = new WeatherServiceSpy();

// act await service.GetCityWeatherAsync(CityName);

// assert service.HasBeenCalled().GetCityWeatherAsync(); service.HasBeenCalled().GetCityWeatherAsync(CityName); service.HasBeenCalled().Once().GetCityWeatherAsync(); service.HasBeenCalled().Once().GetCityWeatherAsync(CityName);

var invokation = service.GetCalls().First().GetCityWeatherAsync(); Assert.AreEqual("GetCityWeatherAsync", invokation.Name);}

Page 23: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

[TestMethod]public async Task StubTest(){ // arange var service = new WeatherServiceStub(); var dummy = new WeatherInfo(); service .AddHandlers() .GetCityWeatherAsync(cityName => Task.FromResult(dummy));

// act var actual = await service.GetCityWeatherAsync(CityName);

// assert Assert.AreEqual(dummy, actual);}

Page 24: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

[TestMethod]public async Task MockTest(){ // arange var service = new WeatherServiceMock(); service .AddVerifications() .GetCityWeatherAsync(CityName) .GetCurrentCityWeatherAsync(CityName);

// act await service.GetCityWeatherAsync(CityName); await service.GetCurrentCityWeatherAsync(CityName);

// asserts service.VerifyAll();}

Page 25: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

jMock

Page 26: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

Evitar usos de estáticos o singletons

Page 27: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

Page 28: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

Simplificar constructores No usar “new” No asignar algo que no sea un atributo No usar “Initializer” No usar condicionales o bucles

Page 29: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

Test positivo y negativo

Page 30: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

Ventajas de los unit tests Encontrar bugs pronto Red de seguridad Documentación Mejor diseño

Page 31: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

Limitaciones de los unit tests No detectan problemas de: Integración Performance … No todo puede ser testeado fácilmente: Multi-threading Algoritmos no deterministas …

Page 32: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

Técnicas para hacer unit testing TDD ATDD BDD

Page 33: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

Métricas de código Code Coverage Cyclomatic Complexity

Page 34: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

Preguntas

Page 35: Codemotion 2015 - Unit Testing

MADRID · NOV 27-28 · 2015

Muchas graciasFernando [email protected]@tokiota.com