Highlighted
Trusted Contributor.
Trusted Contributor.
241 views

Unit-Testing Framework - Limit db connect/disconnect calls

Jump to solution

When unit-testing code, when I want to write code for db connect and db disconnect in the current framework, there is no place holder where we can do this a single time for all the test cases for one program.

In the unit-testing framework we have entry points which define the program flow, I’ve mentioned the entry points below in the order that it is executed:

  1. entry MFU-TC-METADATA
  2. entry MFU-TC-PREFIX
  3. entry MFU-TC-SETUP-PREFIX
  4. entry MFU-TC-TEARDOWN

All these entry points are executed each time a test case is run, so when we write a code for db connect and disconnect in the test program( in any one of the above sections), and I’ve 10 rows in the input CSV file, then the db connect and db disconnect statements will be executed 10 times.This will cause a performance issue down the line when we are running 100s of test cases for a single program. Is there a way we can control the flow or have a section which is executed only once before and after execution of all test cases written?

0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

Re: Unit-Testing Framework - Limit db connect/cisconnect calls

Jump to solution

I was able to create an example, based on the example in the documentation for data-driven Unit Tests that demonstrates one way that you could do this.

I have attached a zip file here. Unzip it to a folder on your system then open up a Visual COBOL command prompt and change directories to that folder and run the doit.bat file to compile and run the test.

Since the test harness program is called once for each record in the csv file and the test harness maintains state between the calls you can add a header and trailer record to the csv file which will tell the program that it should do a connect or disconnect. The connect and disconnect will also be part of the test results.

My example here uses a simple external file instead of a database but it should work the same way. When the CONNECT record is passed to the program it will perform a paragraph that opens the file for output and writes a record to it. The DISCONNECT record will write a record and close the file. The actual test being called will write records to the file without the need to open and close it each time.

I hope this workaround helps you...

Thanks.

View solution in original post

4 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

Re: Unit-Testing Framework - Limit db connect/cisconnect calls

Jump to solution

Hi @StompinBob ,

 

My apologies for the delay in responding. You are correct that the Unit Testing framework does not directly support the type of pre-all-tests or post-all-tests event that you would like to use for one-time actions like connecting to/disconnecting from a database. I've received a suggestion for an option that might provide a way to do what you want. I'll provide an update here after I've had a chance to try it out.

Blair McDonald

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

Re: Unit-Testing Framework - Limit db connect/cisconnect calls

Jump to solution

I was able to create an example, based on the example in the documentation for data-driven Unit Tests that demonstrates one way that you could do this.

I have attached a zip file here. Unzip it to a folder on your system then open up a Visual COBOL command prompt and change directories to that folder and run the doit.bat file to compile and run the test.

Since the test harness program is called once for each record in the csv file and the test harness maintains state between the calls you can add a header and trailer record to the csv file which will tell the program that it should do a connect or disconnect. The connect and disconnect will also be part of the test results.

My example here uses a simple external file instead of a database but it should work the same way. When the CONNECT record is passed to the program it will perform a paragraph that opens the file for output and writes a record to it. The DISCONNECT record will write a record and close the file. The actual test being called will write records to the file without the need to open and close it each time.

I hope this workaround helps you...

Thanks.

View solution in original post

Highlighted
Trusted Contributor.
Trusted Contributor.

Re: Unit-Testing Framework - Limit db connect/cisconnect calls

Jump to solution

Thanks very much for the workaround!  We will try it out and advise.

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

Re: Unit-Testing Framework - Limit db connect/cisconnect calls

Jump to solution

Just to follow-up on this topic, Development will be adding new functionality to the Unit Testing framework which will allow for setup and teardown entry points that will be called at the beginning and end of a data-driven unit test run. This will be included in the next major product release which is 6.0 due to be GA in summer.

- MFU-TC-SETUP-DD-PREFIX - Prefix for the data driven setup entry-point
- MFU-TC-TEARDOWN-DD-PREFIX - Prefix for the data driven teardown entry-point

entry MFU-TC-SETUP-DD-PREFIX & "TESTFLYER".
      perform 100-open-file
      goback.
 
entry MFU-TC-TEARDOWN-DD-PREFIX & "TESTFLYER".
      perform 200-close-file
      goback.
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.