codemotion 2015 - unit testing
TRANSCRIPT
Unit Testing El mito de los cero bugsMADRID · NOV 27-28 · 2015
MADRID · NOV 27-28 · 2015
Fernando [email protected]@tokiota.com
MADRID · NOV 27-28 · 2015
Agenda Definición de Unit Test
Estructura Código Testeable Unit Test
EstructuraHerramientas
Conclusiones
MADRID · NOV 27-28 · 2015
¿Qué es una prueba de software?
Input Process OutputCalidad
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
MADRID · NOV 27-28 · 2015
¿Qué es una prueba unitaria?
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
MADRID · NOV 27-28 · 2015
Caraterísticas FIRST Fast Isolated Repeatable Self-Validating Timely
MADRID · NOV 27-28 · 2015
Caraterísticas SECOND Profesional Unitario Automatizable No usa recursos
MADRID · NOV 27-28 · 2015
EstructuraTriple A Arrange Act Assert
MADRID · NOV 27-28 · 2015
EstructuraTriple Cuádruple A Assume Arrange Act Assert
MADRID · NOV 27-28 · 2015
Estructura
MADRID · NOV 27-28 · 2015
Si un método o función es una unidadtengo que desglosarlos al máximo
Vs.
MADRID · NOV 27-28 · 2015
MADRID · NOV 27-28 · 2015
¿Puedo escribir mejores tests?
MADRID · NOV 27-28 · 2015
Puedo escribir mejor código
MADRID · NOV 27-28 · 2015
Desacoplar
MADRID · NOV 27-28 · 2015
MADRID · NOV 27-28 · 2015
Usar patrones de diseño Inversion Of Control Abstract Factory …
MADRID · NOV 27-28 · 2015
Test Doubles Dummies Fakes Spies Stubs Mocks
MADRID · NOV 27-28 · 2015
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);}
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);}
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();}
MADRID · NOV 27-28 · 2015
jMock
MADRID · NOV 27-28 · 2015
Evitar usos de estáticos o singletons
MADRID · NOV 27-28 · 2015
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
MADRID · NOV 27-28 · 2015
Test positivo y negativo
MADRID · NOV 27-28 · 2015
Ventajas de los unit tests Encontrar bugs pronto Red de seguridad Documentación Mejor diseño
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 …
MADRID · NOV 27-28 · 2015
Técnicas para hacer unit testing TDD ATDD BDD
MADRID · NOV 27-28 · 2015
Métricas de código Code Coverage Cyclomatic Complexity
MADRID · NOV 27-28 · 2015
Preguntas
MADRID · NOV 27-28 · 2015
Muchas graciasFernando [email protected]@tokiota.com