Carlos Ble

Carlos Ble

I am a professional software developer, I solve problems.

I also teach and mentor developers to build better software.

Working as an independent professional since 2009.

Can I help you?

  • Do you need high quality tailor-made software?
  • Need training on TDD, clean code or refactoring?
  • Is it risky? do you need advise?
  • May I pair with you to write better code?

Events

Upcoming public training courses:

  1. [in English] May 5, 6 & 7
    TDD - Train the Trainers (iSQI)
    Potsdam, Berlin
  2. [en Español] 23, 24 y 25 de Abril.
    Curso de TDD con examen de certificacion iSQI
    Madrid
  3. [en Español] 29, 30 y 31 de Octubre.
    Curso de TDD con examen de certificacion iSQI
    Madrid

Conferences:

  1. I'll be at SocratesUK 2014
  2. I'll be at the London Test Gathering Workshops.

Stub out with lambda

Lambda functions are a great tool to stub out functions. They turn out to be much more clear and easy than using a stub from a isolation/test-double framework. Lambda in Python are used like this in interaction-based tests:

  1.  
  2. class MyTests(unittest.TestCase):
  3. def test_find_users():
  4. stub = MyDAO()
  5. stub.get_users = lambda: ['carlos', 'juan']
  6. sut = UserFinder(stub)
  7. self.assertEquals(
  8. sut.get_users_with_name_like('ca'),
  9. ['carlos'])
  10.  

The method get_users in the class MyDAO is stubbed out as we assign a new function to it. The test gets a much more clear code than using PyMox or other test-doubles framework for stubs.

Moreover Python let us stub out functions from classes that haven't been instantiated yet, which makes it perfect for interception too. The next example is the way I am using the Google App Engine bulkloader to write integration tests:

  1.  
  2. from appengine_django import InstallAppengineHelperForDjango
  3. InstallAppengineHelperForDjango()
  4. from google.appengine.tools import bulkloader
  5.  
  6. def load_data():
  7. args_dict = {'all the values': 'the bulk loader needs',
  8. 'etc': 'etc'}
  9. bulkloader.RequestManager.AuthFunction = lambda x : ('x', 'y')
  10. bulkloader.Run(args_dict)
  11.  

This way the bulkloader runs avoiding the need to manually type in the username and password. RequestManager is a class and AuthFunction is one of its instance members.

Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.
  • http://www.katalog.znajomi.bieszczady.pl/ Kasper

    How much of an fascinating article, preserve producing significant other

  • http://www.navigatumit.com/services/it-network-consulting-outsourcing-services/on-demand-services/ laptop repair sydney

    Nice post!