GTAC registration closed

Posted by Harry Robinson, Software Engineer in Test

Thank you to everyone who applied to attend the Google Test Automation Conference in New York on August 23rd-24th. We received lots of applications for a very limited number of seats, so the choices were not easy.

The sign-up website is now closed, and this week we'll be sending out acceptances and wait list notices. If you get an acceptance, congratulations -- we look forward to seeing you in New York. If you don't receive one, you'll be on the wait list, and we'll let you know if spaces become available. Either way, we'll let you know. If you don't hear from us in the next week, drop us a note.

All the presentations from the conference will be posted on YouTube Google Channel within a New York minute after the speaker leaves the podium (well, maybe 72 New York minutes). We look forward to seeing you at the conference.

Permalink | Links to this post | 4 comments

TotT: Extracting Methods to Simplify Testing

When a method is long and complex, it is harder to test. You can make it easier by extracting methods: finding pieces of code in existing, complex methods (or functions) that can be replaced with method calls (or function calls). Consider the following complicated method:


def GetTestResults(self):
# Check if results have been cached.
results = cache.get('test_results', None)
if results is None:
# No results in the cache, so check the database.
results = db.FetchResults(SQL_SELECT_TEST_RESULTS)
# Count passing and failing tests.
num_passing = len([r for r in results if r['outcome'] == 'pass'])
num_failing = len(results) - num_passing
return num_passing, num_failing

This method is difficult to test because it not only relies on a database, but also on a cache. In addition, it performs some post processing of the retrieved results. The first hint that this method could use refactoring is the abundance of comments. Extracting sections of code into well-named methods reduces the original method's complexity. When complexity is reduced, comments often become unnecessary. For example, consider the following:


def GetTestResults(self):
results = self._GetTestResultsFromCache()
if results is None:
results = self._GetTestResultsFromDatabase()
return self._CountPassFail(results)

def _GetTestResultsFromCache(self):
return cache.get('test_results', None)

def _GetTestResultsFromDatabase(self):
return db.FetchResults(SQL_SELECT_TEST_RESULTS)

def _CountPassFail(self, results):
num_passing = len([r for r in results if r['outcome'] == 'pass'])
num_failing = len(results) - num_passing
return num_passing, num_failing

Now, tests can focus on each individual piece of the original method by testing each extracted method. This has the added benefit of making the code more readable and easier to maintain.

(Note: Method extraction can be done for you automatically in Python by the open-source refactoring browser BicycleRepairMan, and in Java by several IDEs, including IntelliJ IDEA and Eclipse.)

Remember to download this episode of Testing on the Toilet and post it in your office.
 

Permalink | Links to this post | 2 comments