Automated Testing §Vít Svoboda §2015 Topics •TDD •Frameworks •Test Life Cycle •Asserting •Mocks & Fakes •Test Coverage •JavaScript §Automated Testing § Automated Testing, TDD •Automation of laborious repetitive testing •Can be repeated every time the code changes. • •Test Driven Development - Tests should be written first, then the code they test. §Automated Testing § •Uvod – testovani, automatizace kvuli urychleni prace, omezeni prostoru pro chyby testera •TDD – testy driv nez kod •Demo: RealLifeExample – jednoduchy test co podchyti OutOfRangeException 1.NameHelperTests – GetUniqueNameTests 2.Prefix delsi nez maximum (Halp 1) 3.Pak opravit Frameworks •Provide a way to describe expectations about code behaviour • §Microsoft Unit Testing Framework •Out of the box in Visual Studio §NUnit •Similar to JUnit •Requires an adapter in Visual Studio •Industry standard §xUnit.net •More "modern" approach •Used to test .NET Core §Automated Testing § •Testovaci frameworky poskytuji funkcionality k psani testu •Je jedno jaky pouzijou Test Body §Automated Testing § AAA – jen popsat 1.Nastavim co potrebuju aby volani otestovalo co potrebuju 2.Testovany volani 3.Overeni vysledku Test Life Cycle §Automated Testing § DEMO: TestLifeCycle 1.Ukazat NUnit nugety 2.Ukazat classu s testama 3.Ukazat parent classu 4.Ukazat output Asserting §Automated Testing Asserting •Ocekavani co se melo stat – nemusi byt spravny •Co nejsemantictejsi •Popsat Assert classy Asserting §Automated Testing Ukazat failnuty testy v TestLifeCycle Code Coverage §Automated Testing Code Coverage •Ktery radky produkcniho kodu byly zavolany pri spousteni nejakyho testu •Vicemene nicnerikajici •Management -> Kvoty -> Testy na gettery a setter •Vyssi edice Visual Studia What to test? •You can test everything! • •Initial cost of the tests •Maintenance cost § vs. •Value of various pieces of code being covered by tests §Automated Testing Na co teda psat testy? DEMO: Lecture8Canon – CultureHelper •Proklikat slavovu sln a ukazat na co urcite nema smysl psat testy •Entity •Konfigurace MVC •EntityFramework – urcite maji spoustu svych testu •Vyrobit testovaci project na .Web •Pridat do nej nuget Nunit •CultureHelperTests.cs •GetSupportedCultureTests TestFixture •First test – fix references, run (Halp 3) •Couple other tests – ukazat jak se opakuji (Halp 4) •TestCase – (Halp 5) •Ukazat ze params [] generuje blby jmeno – fix pomoci TestName parametru •TestCase omezeny na konstanty •TestCaseSource – (Halp 6) Co dal testovat? •Slavo v modelech nema logiku •Slavo ma logiku v Controllerech – tam ji nemuzu testovat DEMO: Lecture8Canon – Refactor GamesController.Index 1.Ukazat co bych musel vsechno v testu vytvorit abych mohl tu logiku testovat 2.Vytahnout do novyho projektu 3.Fix references 4.Presunout DbContext 5.Vytvorit IQueryable extension (Halp 7) 6.Vytvorit GamesService (Halp 8) 7.Vytvorit .Repository.Tests project 8.Napsat test na extension metodu •games = null – nejdriv mylny ocekavani ze to vrati null •pak opravit metodu I test (Halp 9) 9.Napsat dalsi test (Halp 10) Jak otestovat GamesService? Mohl bych test nechat vyrobit si databazi •Pomale •“testuje” mnohem vic kodu, nez chci Mocks & Fakes §Automated Testing Mocks •Umoznujou nahradit nejakej object “fejkem” DEMO: Lecture8Canon – Moq 1.Stahnout nuget Moq 2.Namockovat GamesDbContext pro GamesService (Halp 11) 3.Vyseparovat mockovani DbSetu do metody (Halp 12) •Pokud bude cas, pobavit se o arrange / act / assert a mockovani vyhodit do SetUp metody. •Pokud bude cas, pohavit se o fejk implementaci interface •Pokud bude cas pobavit se o testovani protected a internal funkci JavaScript Testing •Fundamentally same as C# •Tooling differs • •Test frameworks – Jasmine, tape, mocha, etc. •Test runners – browser, Node.js, Gulp, Grunt, Karma §Automated Testing Testovani JavaScriptu •Psani testu samotnych stejne jako v C# •Jasmine – specs – Module ‘a’ can ‘b’: it does stuff DEMO: FrontEndTests 1.Romanuv project nacvakanej do Visual Studia a webpack zabaleny do gulp tasku 2.Napsat jeden testik do TodoModel.spec.js (Halp 13) 3.Co ted s nim? Spustit primo v gulpu (Halp 14) 4.Dulezity, kdo interpretuje kod (browser/node) 5.Muzem refaktorovat TodoModel aby dostal localStorage z venku (ale to delat nebudeme) 6.Muzeme testy poustet v necem blizsim produkcnimu prostredi 7.Karma config (Halp karma.conf.js) •Vysvetlit headless browser •Vysvetlit ze I na testy musim poustet webpack 8.A pustit v gulpu (Halp 15) 9.Napsat dalsi testik – handles null (Halp 16) 10.Ukazat, ze to pada a spyOn localStorage (Halp 17) •Pokud bude cas, ukazat interaktivni mod (singleRun: false) •Pokud bude cas, ukazat jiny browser (Chrome) •Pokud bude cas, ukazat debug Write tests! §