+ Start a Discussion
NickDevNickDev 

Using common test class inside another test class

Hi,

I've written an apex Test class to test my Apex code before deployment.

I have a general test class I created which works to create a specific record of data, but its quite complex due to the amount of other bits of data that need created first due to lookups etc..

So in my new test class I would like to call this first test class to generate some data.

I can see on https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_utility_classes.htm it says to just call it using TestClass.Method() which I have tried but because the class I am calling has Test.startTest(); it complains the test is already running?

Also, I would obviously only like my new test class to carry on once the class I am calling has finished and there is data. Is this possible or am I doing something wrong?

Thank you!
 
AlexCAlexC
Hi Nick,

Your best approach to this is to create a test utility class. This is where your complex data creation will happen, and can be called by other test classes. There is a Trailhead module that goes through this scenario, which should be able to provide you with more insight: https://trailhead.salesforce.com/modules/apex_testing/units/apex_testing_data

I hope this assists you with your problem,

Alex