Unit testing en iOS @ MobileCon Galicia

2,094 views
1,986 views

Published on

Unit testing en iOS @ MobileCon Galicia

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,094
On SlideShare
0
From Embeds
0
Number of Embeds
511
Actions
Shares
0
Downloads
24
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Unit testing en iOS @ MobileCon Galicia

    1. 1. Unit Testing en iOS @hpique
    2. 2. Así terminan los programadores que no hacen unit testing
    3. 3. Agenda• Unit Testing• OCUnit• GHUnit• Profit!
    4. 4. Unit TestingTesteo de unidades mínimas de código mediante código
    5. 5. - (void)testColorFromHex { NSString* colorString = @"#d34f01"; UIColor* color = [colorString colorFromHex]; const CGFloat *c = CGColorGetComponents(color.CGColor); STAssertEquals(c[0], 211/255.0f, colorString, @""); STAssertEquals(c[1], 79/255.0f, colorString, @""); STAssertEquals(c[2], 1/255.0f, colorString, @""); STAssertEquals(CGColorGetAlpha(color.CGColor), 1.0f,colorString, @"");}
    6. 6. Razones para no hacer unit testing
    7. 7. ...
    8. 8. RazonesExcusas parano hacer unit testing
    9. 9. Excusas
    10. 10. Excusas• “No lo necesito”
    11. 11. Excusas• “No lo necesito”• “El plazo es muy ajustado”
    12. 12. Excusas• “No lo necesito”• “El plazo es muy ajustado”• “No aplica para este proyecto”
    13. 13. Excusas• “No lo necesito”• “El plazo es muy ajustado”• “No aplica para este proyecto”• “Unit testing en XCode apesta”
    14. 14. Razones para sí hacer unit testing
    15. 15. Razones para sí hacer unit testing• Corregir bugs antes
    16. 16. Razones para sí hacer unit testing• Corregir bugs antes• Refinar el diseño
    17. 17. Razones para sí hacer unit testing• Corregir bugs antes• Refinar el diseño• Facilitar cambios
    18. 18. Razones para sí hacer unit testing• Corregir bugs antes• Refinar el diseño• Facilitar cambios• Documentación útil
    19. 19. Razones para sí hacer unit testing• Corregir bugs antes• Refinar el diseño• Facilitar cambios• Documentación útil• Reducir tiempo de testeo
    20. 20. ¿Cuándo?
    21. 21. ¿Cuándo?• Lo antes posible (TDD)
    22. 22. ¿Cuándo?• Lo antes posible (TDD)• En paralelo
    23. 23. ¿Cuándo?• Lo antes posible (TDD)• En paralelo• Al corregir bugs
    24. 24. Definiciones Test Suite SetUpTest Case Test Case Test Case TearDown
    25. 25. Agenda• Unit Testing• OCUnit• GHUnit• Profit!
    26. 26. OCUnit• Framework de Unit Testing para Obj-C• Único framework de testeo integrado nativamente en XCode
    27. 27. +N
    28. 28. #import <SenTestingKit/SenTestingKit.h>@interface HelloWorldTests : SenTestCase@end@implementation HelloWorldTests- (void)setUp{ [super setUp]; // Set-up code here.}- (void)tearDown{ // Tear-down code here. [super tearDown];}- (void)testExample{ STFail(@"Unit tests are not implemented yet in HelloWorldTests");}@end
    29. 29. Escribiendo unit tests con OCUnit• Cada Test Suite es una clase que hereda de SenTestCase• Cada Test Case debe ser un método con el prefijo test• setUp y tearDown son opcionales
    30. 30. +U
    31. 31. Console output2011-12-09 12:43:01.394 HelloWorld[2858:fb03] Applications are expected to have aroot view controller at the end of application launchTest Suite All tests started at 2011-12-09 11:43:01 +0000Test Suite /Users/hermespique/Library/Developer/Xcode/DerivedData/HelloWorld-ezilismrgmrzecbbsndapbyeczre/Build/Products/Debug-iphonesimulator/HelloWorldTests.octest(Tests) started at 2011-12-09 11:43:01 +0000Test Suite HelloWorldTests started at 2011-12-09 11:43:01 +0000Test Case -[HelloWorldTests testExample] started./Users/hermespique/Documents/workspace/HelloWorld/HelloWorldTests/HelloWorldTests.m:33: error: -[HelloWorldTests testExample] : Unit tests are notimplemented yet in HelloWorldTestsTest Case -[HelloWorldTests testExample] failed (0.000 seconds).Test Suite HelloWorldTests finished at 2011-12-09 11:43:01 +0000.Executed 1 test, with 1 failure (0 unexpected) in 0.000 (0.000) secondsTest Suite /Users/hermespique/Library/Developer/Xcode/DerivedData/HelloWorld-ezilismrgmrzecbbsndapbyeczre/Build/Products/Debug-iphonesimulator/HelloWorldTests.octest(Tests) finished at 2011-12-09 11:43:01 +0000.Executed 1 test, with 1 failure (0 unexpected) in 0.000 (0.000) secondsTest Suite All tests finished at 2011-12-09 11:43:01 +0000.Executed 1 test, with 1 failure (0 unexpected) in 0.000 (0.001) seconds
    32. 32. OCUnit MacrosSTAssertEqualObjects(a1, a2, description, ...)STAssertEquals(a1, a2, description, ...)STAssertEqualsWithAccuracy(a1, a2, accuracy, description, ...)STFail(description, ...)STAssertNil(a1, description, ...)STAssertNotNil(a1, description, ...)STAssertTrue(expr, description, ...)STAssertTrueNoThrow(expr, description, ...)STAssertFalse(expr, description, ...)STAssertFalseNoThrow(expr, description, ...)STAssertThrows(expr, description, ...)STAssertThrowsSpecific(expr, specificException, description, ...)STAssertThrowsSpecificNamed(expr, specificException, aName, description, ...)STAssertNoThrow(expr, description, ...)STAssertNoThrowSpecific(expr, specificException, description, ...)STAssertNoThrowSpecificNamed(expr, specificException, aName, description, ...)
    33. 33. OCUnit MacrosSTAssertTrue(expr, description, ...)STAssertFalse(expr, description, ...)STAssertNil(a1, description, ...)STAssertNotNil(a1, description, ...)STAssertEqualObjects(a1, a2, description, ...)STAssertEquals(a1, a2, description, ...)STFail(description, ...)STAssertThrows(expr, description, ...)
    34. 34. Agenda• Unit Testing• OCUnit• GHUnit• Profit!
    35. 35. GHUnit• Framework de Unit Testing para Obj-C• Open-source: github.com/gabriel/gh-unit• GUI!• Compatible con OCUnit
    36. 36. #import <GHUnitIOS/GHUnit.h>@interface ExampleTest : GHTestCase@end@implementation ExampleTest- (BOOL)shouldRunOnMainThread { return NO;}- (void)setUpClass { // Run at start of all tests in the class}- (void)setUp { // Run before each test method}- (void)tearDown { // Run after each test method}- (void)tearDownClass { // Run at end of all tests in the class}- (void)testFoo { NSString *a = @"foo"; GHAssertNotNil(a, nil);}@end
    37. 37. GHUnit MacrosGHAssertNoErr(a1, description, ...) GHAssertEquals(a1, a2, description, ...)GHAssertErr(a1, a2, description, ...) GHAbsoluteDifference(left,right)GHAssertNotNULL(a1, description, ...) (MAX(left,right)-MIN(left,right))GHAssertNULL(a1, description, ...) GHAssertEqualsWithAccuracy(a1, a2,GHAssertNotEquals(a1, a2, description, ...) accuracy, description, ...)GHAssertNotEqualObjects(a1, a2, desc, ...) GHFail(description, ...)GHAssertOperation(a1, a2, op, GHAssertNil(a1, description, ...)description, ...) GHAssertNotNil(a1, description, ...)GHAssertGreaterThan(a1, a2, GHAssertTrue(expr, description, ...)description, ...) GHAssertTrueNoThrow(expr,GHAssertGreaterThanOrEqual(a1, a2, description, ...)description, ...) GHAssertFalse(expr, description, ...)GHAssertLessThan(a1, a2, description, ...) GHAssertFalseNoThrow(expr,GHAssertLessThanOrEqual(a1, a2, description, ...)description, ...) GHAssertThrows(expr, description, ...)GHAssertEqualStrings(a1, a2, GHAssertThrowsSpecific(expr,description, ...) specificException, description, ...)GHAssertNotEqualStrings(a1, a2, GHAssertThrowsSpecificNamed(expr,description, ...) specificException, aName,GHAssertEqualCStrings(a1, a2, description, ...)description, ...) GHAssertNoThrow(expr, description, ...)GHAssertNotEqualCStrings(a1, a2, GHAssertNoThrowSpecific(expr,description, ...) specificException, description, ...)GHAssertEqualObjects(a1, a2, GHAssertNoThrowSpecificNamed(expr,description, ...) specificException, aName, description, ...)
    38. 38. GHUnitAsyncTestCase#import <GHUnitIOS/GHUnit.h>@interface AsyncTest : GHAsyncTestCase { }@end@implementation AsyncTest- (void)testURLConnection { [self prepare]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:requestdelegate:self startImmediately:YES]; [self waitForStatus:kGHUnitWaitStatusSuccess timeout:10.0];}- (void)connectionDidFinishLoading:(NSURLConnection *)connection { [self notify:kGHUnitWaitStatusSuccess forSelector:@selector(testURLConnection)];}@end
    39. 39. Configurar GHUnit1. Crear target2. Agregar GHUnitiOS.framework3. Modificar Other Linker Flags4. Cambiar de AppDelegate
    40. 40. 1. Crear target
    41. 41. 1. Crear target
    42. 42. 2. AgregarGHUnitiOS.framework• Primero debemos hacer build del framework1. Descargar de github y descomprimir2. > cd gh-unit/Project-iOS3. > make
    43. 43. 3. Modificar Other Link Flags• Agregar -all_load• Agregar -objC
    44. 44. 4. Cambiar de AppDelegateint main(int argc, char *argv[]){ @autoreleasepool { return UIApplicationMain(argc,argv, nil, @"GHUnitIOSAppDelegate"); }}
    45. 45. +B
    46. 46. OCUnit vs GHUnit OCUnit GHUnitIntegración con Built-in Manual XCode Contextual / Resultados Consola / GUI Consola Más macrosProgramación GHAsyncTestCase Todo, selección o Ejecución Todo fallidos
    47. 47. ¡Gracias! @hpique

    ×