user1958532
user1958532

Reputation: 123

how to write a custom assert Python

I am planning to split my one big test file to smaller test based on the part of code it tests. And I have a custom assert function for one of my tests. If I split them out to a separate file, how should I import them to other test files.

TestSchemacase:

class TestSchemaCase(unittest.TestCase):
    """
    This will test our schema against the JSONTransformer output
    just to make sure the schema matches the model
    """
    # pylint: disable=too-many-public-methods


    _base_dir = os.path.realpath(os.path.dirname(__file__))
    def assertJSONValidates(self, schema, data):
        """
        This function asserts the validation works as expected

        Args:
            schema(dict): The schema to test against
            data(dict): The data to validate using the schema
        """
        # pylint: disable=invalid-name
        validator = jsonschema.Draft4Validator(schema)
        self.assertIsNone(validator.validate(data))


    def assertJSONValidateFails(self, schema, data):
        """
        This function will assertRaises an ValidationError Exception
        is raised.

        Args:
            schema(dict): The schema to validate from
            data(dict): The data to validate using the schema
        """
        # pylint: disable=invalid-name
        validator = jsonschema.Draft4Validator(schema)
        with self.assertRaises(jsonschema.ValidationError):
            validator.validate(data)

My question is,1. When I try to import them I get an Import error with No module name found. I am breaking the TestValidation to mentioned small files. 2. I know I can raise Validation error in the assertJSONValidateFails but what should I return in case of validation pass.

   tests/schema
├── TestSchemaCase.py
├── TestValidation.py
├── __init__.py
└── models
    ├── Fields
    │   ├── TestImplemen.py
    │   ├── TestRes.py
    │   └── __init__.py
    ├── Values
    │   ├── TestInk.py
    │   ├── TestAlue.py
    │   └── __init__.py
    └── __init__.py

3.And is this how we should inherit them? class TestRes(unittest.TestCase, TestSchemaCase):

Thanks for your time. Sorry for the big post

I did see the post, But that doesn't solve the problem.

Upvotes: 0

Views: 1304

Answers (1)

Roland Smith
Roland Smith

Reputation: 43495

I would suggest using a test framework that doesn't force you to put your tests in classes, like pytest.

Upvotes: 0

Related Questions