Create and run a project

How to interact with CAT from Python?

We expect you already have datasmartly_cat package installed. All functionality is implemented in cat module. Import it like this:

from datasmartly_cat import cat as cat

Create a CAT project file

First, you must have a CAT project file. If you don’t want to create it from scratch, you can use new_project function. It will help you create a new CAT project based on templates. Use get_project_templates function to get list of available templates. This is described in detail in our tutorial.

Working with a project

The easiest way how to evaluate all the tests from your CAT project is to use invoke_project function. If there is exactly one .cat.yaml file in your current working directory, you don’t even have to specify any parameters:

cat.invoke_project()

If you have more .cat.yaml files in your current working directory or your project file is elsewhere, you need to specify a path to your project file (or directory, again - if it contains only one .cat.yaml file):

cat.invoke_project('/home/grace/dwh/testing/smoke_tests.cat.yaml')

The invoke_project function reads your project files, loads all data sources and tests (including those from external locations, such as MS Excel sheets, databases or other YAML files), evaluates them and returns an TestExecutionSummary object.

Test results

TestExecutionSummary contains (not only) these interesing properties. Please note the the naming conventions and object types are NOT typical Python stuff, as they come from underlying .NET implementation. This can change in future versions of Python intgration - we’ll be happy to “translate” these, if needed.

TestExecutionSummary contains properties describing the tests run as a whole: StartedOn, FinishedOn, Duration, PassedCount, FailedCount, ErrorCount, InconclusiveCount, WarningCount, SkippedCount, TotalCount, PassRate

Another interesting property is Results - it contains information about all evaluated tests. It is a collection (again, .NET IEnumerable, don’t expect Python list yet) of TestOutput objects. TestOutput has these interesting properties, describing evaluation of one single test: TestResult, Message, RawMessage, StartTime, EndTime, Duration, ExceptionMessage, TestName, TestFullName, TestSuite, TestCase, Description, FirstDataSource, FirstQuery, SecondDataSource, SecondQuery, Expectation, NumberOfErrors, ThreadNumber.

Another interesting one is TestDefinition - it contains information about everything you specified in the definition of your test (including e.g., custom properties/columns). Note that some test definition attribues (such as TestName) are also accessible directly from TestOutput (for convenience).

If you need some non-trivial logic based on results, you typically iterate through the results like this:

results = cat.invoke_project(cat_project_path)
for testResult in results.TestResults:
     # do something

Running tests without reloading the project

Please note the invoke_project is a shortcut. It is the same as calling

  • open_project: It parses the project files, loads all data sources and tests, including those from external locations and generated from metadata. But it does NOT evaluate the tests.

  • invoke_tests: It expects a project is already open (using open_project) and only runs all the tests.

In some scenarios, it might make sense to use this approach - open a project and then invoke tests as many times as needed, without reloading the project information.