Index: chrome/test/mini_installer/test_installer.py |
diff --git a/chrome/test/mini_installer/test_installer.py b/chrome/test/mini_installer/test_installer.py |
index a59bf9164074f3706728b0b4042490dfee34f2c3..2c41e934bb92862b2a6d102495abf928dc57ca1b 100644 |
--- a/chrome/test/mini_installer/test_installer.py |
+++ b/chrome/test/mini_installer/test_installer.py |
@@ -13,8 +13,8 @@ import argparse |
import json |
import os |
import subprocess |
+import unittest |
-import settings |
import verifier |
@@ -34,6 +34,73 @@ class Config: |
self.tests = [] |
+class InstallerTest(unittest.TestCase): |
+ """Tests a test case in the config file.""" |
+ |
+ def __init__(self, test, config): |
+ """Constructor. |
+ |
+ Args: |
+ test: An array of alternating state names and action names, starting and |
+ ending with state names. |
+ config: The Config object. |
+ """ |
+ super(InstallerTest, self).__init__() |
+ self.test = test |
+ self.config = config |
+ |
+ def runTest(self): |
+ """Run the test case.""" |
+ test = self.test |
+ |
+ # |test| is an array of alternating state names and action names, starting |
+ # and ending with state names. Therefore, its length must be odd. |
+ self.assertEqual(len(test) % 2, 1, 'The length of test array must be odd') |
+ |
+ self._RunResetCommand() |
+ |
+ current_state = test[0] |
+ self._VerifyState(current_state) |
+ |
+ for i in range(1, len(test), 2): |
+ action = test[i] |
+ self._RunCommand(self.config.actions[action]) |
+ |
+ current_state = test[i + 1] |
+ self._VerifyState(current_state) |
+ |
+ def __str__(self): |
+ return 'Test: %s' % ('-> '.join(self.test)) |
+ |
+ def shortDescription(self): |
+ """Returns None as the short description to suppress its printing. |
+ |
+ The default implementation of this method returns the docstring of the |
+ runTest method, which is not useful since it's the same for every test case. |
+ The description from the __str__ method is informative enough. |
+ """ |
+ return None |
+ |
+ def _VerifyState(self, state): |
+ """Verifies that the current machine states match the given machine states. |
+ |
+ Args: |
+ state: The current state. |
+ """ |
+ try: |
+ verifier.Verify(self.config.states[state], self) |
+ except AssertionError as e: |
+ error_msg = "In state '%s', %s" % (state, e) |
robertshield
2013/08/06 21:42:22
It seems like the whole config struct is passed do
sukolsak
2013/08/06 23:43:06
True, but I would like to keep the assertions as s
|
+ raise AssertionError(error_msg) |
+ |
+ def _RunCommand(self, command): |
+ subprocess.call(command, shell=True) |
+ |
+ def _RunResetCommand(self): |
+ # TODO(sukolsak): Need to figure how exactly we want to reset. |
+ pass |
+ |
+ |
def MergePropertyDictionaries(current_property, new_property): |
"""Merges the new property dictionary into the current property dictionary. |
@@ -100,54 +167,16 @@ def ParseConfigFile(filename): |
return config |
-def VerifyState(config, state): |
- """Verifies that the current machine states match the given machine states. |
- |
- Args: |
- config: A Config object. |
- state: The current state. |
- """ |
- # TODO(sukolsak): Think of ways of preserving the log when the test fails but |
- # not printing these when the test passes. |
- print settings.PRINT_STATE_PREFIX + state |
- verifier.Verify(config.states[state]) |
- |
- |
-def RunCommand(command): |
- print settings.PRINT_COMMAND_PREFIX + command |
- subprocess.call(command, shell=True) |
- |
- |
-def RunResetCommand(): |
- print settings.PRINT_COMMAND_PREFIX + 'Reset' |
- # TODO(sukolsak): Need to figure how exactly we want to reset. |
- |
- |
def Test(config): |
"""Tests the installer using the given Config object. |
Args: |
config: A Config object. |
""" |
+ suite = unittest.TestSuite() |
for test in config.tests: |
- print settings.PRINT_TEST_PREFIX + ' -> '.join(test) |
- |
- # A Test object is an array of alternating state names and action names. |
- # The array starts and ends with states. Therefore, the length must be odd. |
- assert(len(test) % 2 == 1) |
- |
- RunResetCommand() |
- |
- current_state = test[0] |
- VerifyState(config, current_state) |
- # TODO(sukolsak): Quit the test early if VerifyState fails at any point. |
- |
- for i in range(1, len(test), 2): |
- action = test[i] |
- RunCommand(config.actions[action]) |
- |
- current_state = test[i + 1] |
- VerifyState(config, current_state) |
+ suite.addTest(InstallerTest(test, config)) |
+ unittest.TextTestRunner(verbosity=2).run(suite) |
def main(): |