0

I have what I hope to be a pretty general question. I am testing XML files and validating specific tags. I didn't want to re-write the code for each field so I am trying to reuse the tag checking code. However, when I try to do so

Here is the "ValidateField" class that I am trying to use the UnitTest Framework in. This is the code I am trying to use. I am simply trying to call it as follows. What I get is that the unit test framework ran 0 tests. I am sucessfully using the unit test framework elsewhere in my program where I am not trying to reuse a class. Anyone now where I am going astray?

Here is the output I get:

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

----------------------------------------------------------------------

Here is the class/Method where I am making the call to the reusable class

import Common.ValidateField


class Tests():
    def test_testCreated(self):
        validate = Common.ValidateField.ValidateField()
        validate.test_field('./created')

Here is the reusable class using the unit test framework.

import unittest
import Main
import Common.Logger


class ValidateField(unittest.TestCase):
    def test_field(self, xpath):
        driver = Main.ValidateDriver().driver
        logger = Common.Logger.Logger
        tag = []

    for t in driver.findall("'" + xpath + "'"):
        tag.append(t)

    # Test to see if there is more than one tag.
    if len(tag) > 1:
        self.assertTrue(False, logger.failed("There is more than one " + "'" + t.text + "'" + " tag."))
    else:
        # Test for a missing tag.
        if len(tag) <= 0:
            self.assertTrue(False, logger.failed("There is no " + "'" + t.text + "'" + " tag."))
            # Found the correct number of tags.
        self.assertTrue(True, logger.passed("There is only one " + "'" + t.text + "'" + " tag."))

    # Test to make sure there is actually something in the tag.
    if t.text is None:
        self.assertTrue(False, logger.failed("There is a " + "'" + t.text + "'" + " tag, but no value exists"))
Paddyd
  • 1,870
  • 16
  • 26
user2643864
  • 641
  • 3
  • 11
  • 24

2 Answers2

0

The class where you're using the "reusable class" doesn't inherit from unittest.TestCase, so won't be picked up by the test runner. Instead you have the "reusable class" itself inheriting from that.

One possible solution is to make your main class a subclass of the reusable class, then refer to the common method via self. Of course, you're going to have to give that method a different name, as if it's prefixed with test_ the runner will think it's a test itself, which it isn't, and try to run it. So:

from common import ValidateField

class Tests(ValidateField):
    def test_testCreated(self):
        self.field_test('./created')
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thanks Daniel. Originally I was inheriting from unittest.TestCase where I was trying to use the "reusable class". However, everytime I tried to do this I couldn't get it to load the tests and would get the following error: – user2643864 Aug 29 '13 at 16:30
  • Traceback (most recent call last): File "C:\Users\sharmon\PycharmProjects\DriverValidator\Tests\TestCreated.py", line 7, in test_testCreated return validate.ValidateField().test_field('./created') File "C:\Python27\lib\unittest\case.py", line 191, in __init__ (self.__class__, methodName)) ValueError: no such test method in : runTest ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (errors=1) – user2643864 Aug 29 '13 at 16:45
  • I then thought I would have it inherit from the "reusable class" as you stated but then I get the following error. Please let me know if I can provide more information. from Common import ValidateField class Tests(ValidateField): def create_test(self): validate = self.ValidateField() validate.field_test('./created') – user2643864 Aug 29 '13 at 16:46
  • If you inherit ValidateField, you don't need to create object validate = self.ValidateField(). You just have to put self.field_test('./created') i guess. – Katsu Aug 29 '13 at 17:59
0

It appears that there is a problem with your indentation? Looking at the definition of test_field method, it appears that only the first three statements are correctly indented and are within the definition of the test methd. The rest of the code is outside the body of the test_function and hence nothing is going to asserted.

This probably may be the reason why you are seeing ran 0 tests - since there is nothing to be tested within the test_field function.

Correct the indentation to make sure that all the remaining code also appears under the definition of test_function. Hopefully, it should work, else we will take another look.

Prahalad Deshpande
  • 4,709
  • 1
  • 20
  • 22
  • Thanks Prahalad. When I pasted the code into this forum, the indentation isn't showing correcty. However, it is indented properly under the definition. – user2643864 Aug 29 '13 at 16:49