B2G/QA/Automation/Style Guide/Avoiding Duplication

From MozillaWiki
< B2G‎ | QA‎ | Automation‎ | Style Guide
Jump to: navigation, search

Between 2 tests

Sometimes, you might want to copy and paste 1 test in order to test a sightly similar scenario.

Same test, different data

If the only thing that changes is the data you provide to the test, you can use the @parameterized decorator. Here's an example:

@parameterized("zero_length_file", 'image_formats/x05.png')
@parameterized("truncated_jpg", 'image_formats/x08.jpg')
def test_gallery_handle_load_corrupt_file(self, filename):
    self.push_resource(filename)

    gallery = Gallery(self.marionette)
    gallery.launch()

    self.assertTrue(gallery.gallery_items_number == 0)

Tests have a set of steps that are common

Sometimes, you have steps that are repeated in many tests. One way to avoid the duplication here is to group these steps in the page class.

Within 1 page class

Sometimes you might end up repeating the same steps over and over along the Page class. Like in regular software development, don't be afraid to factorize all these steps in a function. For example:

class ContactsDetails(PageRegion):

    def type_email(self, value):
        self._type_in_field(self._add_new_email_locator, self._email_locator, value)

    def type_phone(self, value):
        self._type_in_field(self._add_new_phone_locator, self._phone_locator, value)

    def _type_in_field(self, add_locator, field_locator, value):
        Wait(self.marionette).until(expected.element_present(*add_locator)).tap()
        element = Wait(self.marionette).until(expected.element_present(*field_locator))
        Wait(self.marionette).until(expected.element_displayed(element))
        element.clear()
        element.send_keys(value)