Download - Testing - Is This Even a Thing?
Testing,Is This Even a Thing?NICK GEORGESAM SCHNEIDER
GTM SPORTSWEAR
Why Do We Need Tests?• “You know you are working on clean code when each
routine you read turns out to be pretty much what you expected.”–Ward Cunningham
• Symptoms:• Code Rot• Apathetic Developers• Code Breaking And Having No Idea Why
What is a Test?• An automated way to make sure your code does what
you think it does.• Unit testing involves breaking your program into pieces,
and subjecting each piece to a series of tests.
assert('Strings should equate', assert => { const testString = 'test'; equal(testString, 'test');});
What is in a Test?
• Setup everything you need to perform test.Arran
ge
• Call into testable code.
• (Input)
Act• Verify code• (Output)
Assert
Triple "A" Pattern
test('Can parse a query string that is passed in', assert => { const reader = new QueryStringReader('?name=bill&home=TN'), pairs = reader.getAttributesAndValues();
equal(pairs[0].name, 'name'); equal(pairs[0].value, 'bill'); equal(pairs[1].name, 'home'); equal(pairs[1].value, 'TN');});
test('Clicking buttons changes display', assert => { const s = new SizeChart(analyticsStub, sizingDataStructure); s.display(container);
const cutButtons = container.querySelectorAll('.component-list-picker li'), button = cutButtons[1], contentRegions = container.querySelectorAll('.cut-region'); TestHelper.clickElement(button);
const button1IsSelected = cutButtons[0].classList.contains('selected'), contentRegion1IsShown = contentRegions[0].classList.contains('active'); assert.equal(button1IsSelected, false); assert.equal(contentRegion1IsShown, false);
const button2IsSelected = cutButtons[1].classList.contains('selected'), contentRegion2IsShown = contentRegions[1].classList.contains('active'); assert.equal(button2IsSelected, true); assert.equal(contentRegion2IsShown, true);});
What Do I Get Out of Testing?•Confidence in your code.• Being able to refactor your code.• Being able to refactor legacy code.• Testing your code in production!
• What is legacy code?• “To me, legacy code is simply code without tests.” –Michael
Feathers
How do I get started?1. Choose a Test Framework• Qunit• Mocha• Jasmine
2. Choose a Test Runner• Karma• Chutzpah• Protractor
3. Start Testing!
Final Notes• Test against specific business requirements.
• You shouldn’t be coming up with tests, your stakeholders should!
• Treat tests like production code!
Questions?
Thank You!
• Nick George <[email protected]>• Sam Schneider <[email protected]>
GTM Networking Night Thursday, November 3rd 4 - 7pm