Test Driven Development



Test Driven Development

0 0


unit-testing-presentation


On Github astraldragon / unit-testing-presentation

Test Driven Development

Chris Legault

About Me

  • Graduated from NBCC in 2008
  • Worked at T4G from 2008-2012
  • Currently work at Radian6/Salesforce

What is Unit Testing?

Explanation here

What is Test Driven Development?

Explanation here

Benefits

  • Reduce bugs
  • Confidence
  • Documentation
  • Measurement

Shortcomings

  • Does not test everything
  • False sense of security
  • Overtesting

How?

Write a test Watch it fail Write code Watch it pass Refactor Repeat

Testing Libraries

.NET - NUnit, Visual Studio Testing Framework

Java - JUnit

JavaScript - QUnit, Mocha, Jasmine

Writing a test

Arrange Act Assert Teardown

Sample Test

            describe('Twitter', function() {
  describe("convertUsername", function() {
    it("should create a username link for text in a tweet that begins with @", function() {
      // Arrange
      var tweet = '@salesforce - check this out!';
      // Act
      tweet = Twitter.convertUsername(tweet);
      // Assert
      expect(tweet).toBe('<a href="https://twitter.com/salesforce">@salesforce</a> - check this out!');
    });
  });
});
          

Sample Code

            (function() {
  var Twitter = Twitter || {};
  this.Twitter = Twitter;

  Twitter.convertUsername = function(tweet) {
    return tweet.replace(/[@]+[A-Za-z0-9-_]+/g, function(u) {
      var username = u.replace("@","")
      return u.link("https://twitter.com/" + username);
    });
  }
})();
          

Best Practices

  • Clearly name tests
  • Test components in isolation
  • Mock external dependencies

Pig Latin

We will be making an English to Pig Latin translator

The Rules of Pig Latin

  • A word that begins with a consonant has the first group of consonants moved to the end of the word, and "ay" is added
  • A word that beings with a vowel sound or silent letter has "way" added to the end of the word

Testing in Isolation

We will test code that has an external dependency by using mocks with Moq.

Presentation can be found at https://github.com/astraldragon/nbcc-presentation

Questions?