How to Write Method to Verify Reflection Method Names and Parameters
<p>We were writing a Selenium test core by using Python webdriver. The main idea is to read from a CSV file with the format:</p>

<pre><code>method_name,parameter 1,parameter 2, parameter 3, ..., parameter n
</code></pre>

<p>And then by using reflection, the test core will call the method based on the "method name" and parameters.</p>

<pre><code>def runTest(self):
        try:
            test_case_reader = csv.reader(open(self.file_path, 'rb'), delimiter=',')
            self.logger.info("Running test case %s" % self.file_path)
            for row in test_case_reader:
                if (len(row) &gt; 1):
                    method_name, parameters = row[0], row[1:]
                    parameters = filter(None, parameters)
                    method = getattr(self, method_name)
                    self.logger.info("executing method %s parameters %s" % (method_name, parameters))
                    method(*parameters)
                    self.wait()
        except AssertionError, e:
            self.logger.error(e)
            self.fail("Test case failed: %s" % self.file_path)
</code></pre>

<p>The main idea of this script is to provide a better productivity and ease of usage, since QA won't have to interact with python code to write automation test cases.</p>

<p>But due to human error, sometimes there will be a typo or parameter mismatched.</p>

<p>So I'm going to add a method to verify all CSV files if method names and parameters are correct. Is there any built-in reflection to do this kind of checking?</p>

<h2>Update:</h2>

<p>For the parameters I'd like to check if parameters count match the required parameters of the method. Some of the methods has optional parameters too.</p>

<p>Example:</p>

<p><code>method_1 (parameter1, parameter2, parameter3, parameter4 = None, parameter5 = None)</code></p>

<p>For <code>method_1</code>, valid parameters count are: 3, 4, and 5.</p>

<h2>Answer:</h2>

<p>Not the perfect &amp; tidy solution but this is the method for now and it's working:</p>

<pre><code>def verifyFile(self):
    test_case_reader = csv.reader(open(self.file_path, 'rb'), delimiter=',')
    for row in test_case_reader:
        if (len(row) &lt;= 1):
            break

        method_name, parameters = row[0], row[1:]
        if (method_name == ''):
            break

        parameters = filter(None, parameters)
        if not hasattr(self, method_name):
            self.logger.error("test case '%s' method name '%s': Method not found" % (self.file_path, method_name))
            break
        t = inspect.getargspec(getattr(self, method_name))
        max = len(t.args)
        if (type(t.defaults) == NoneType):
            min = max
        else:
            min = max - len(t.defaults)
        if not min &lt;= len(parameters) + 1 &lt;= max:
            self.logger.error("test case '%s' method name '%s': Parameter count not matches (%d - %d)" % (self.file_path, method_name, min, max))
</code></pre>
