Forum Discussion

sumit0609's avatar
sumit0609
Occasional Contributor
4 years ago
Solved

Tag in Datasource

I have a data source [.csv] with multiple records. I want to be able to make use of tags or other means to filter the data source based on certain criteria and have the Jenkins pick up only the resul...
  • ChrisA's avatar
    ChrisA
    4 years ago

    Hi sumit0609richie ,

     

    Never heard of tags, sorry.  But I think I understand what you are trying to achieve and I do something similar.

    I tend to do this in two ways.  Firstly, specific records in a datasource.  Second, specific sheets in a datasource.

     

    Both of my methods involve a Groovy script to decide what datasource rows to execute when running a test.

     

    Skipping Rows in a Datasource

    In the datasouce (tend to use Excel) itself.  My first column tends to titled 'Run?'.  Valid values are YES, NO or STOP.  This allows me to cherry-pick a subset of tests to run, say for debugging.

    The test itself would look something like:-

     

    (Sorry, just noticed typos in 'individual')

    The key piece here is the 'Run this step?' groovy script.  This is essential an if-else step that decides what to do based on the first column.  E.g.

     

    def runflag = context.expand( '${DataSource#RUNFLAG}' )
    def testNumber = context.expand( '${DataSource#TESTNUMBER}' )
    
    log.info("About to run Test ${testNumber}.");
    
    if(runflag.equals("YES")){
    	
    	log.info("Run Flag is set to YES....");	
    	
    } else if(runflag.equals("NO")) {
    	
    	log.info("Run Flag is set to NO, move onto next scenario....");
    	testRunner.gotoStepByName("End Invidual Test Groovy Script");
    	
    } else if(runflag.equals("STOP")) {
    	
    	log.info("Run Flag is set to STOP, submit no requests.  Move to the final step....");
    	testRunner.gotoStepByName("End of Test Run - Groovy Script");
    	
    }  else if(runflag.equals("")) {
    	
    	log.info("Run Flag is NOT set, move onto next scenario....");
    	testRunner.gotoStepByName("End Invidual Test Groovy Script");
    	
    } else {
    	
    	log.info("ERROR, unexpected run flag value... ${runflag}.");
    	
    }

     

     

    If you're thinking, "what is in the last two groovy scripts?". Don't worry.  They're empty.  I just use them like comments in code and somewhere to 'land' if not running that step.

    I'm going through all as you could have an additional column in your datasource (e.g. CI test) and CI specific groovy step, so you effectively have tagged a row of interest.  

     

    Select Sheet in Datasource

    You can pick which sheet, within a spreadsheet, you want to run during a given test after the test starts.

    I do this for some tests where the test is the same for all environments, but the data differs slightly.

     

    For example, in my datasource spreadsheet, I have the sheets.  Dev, Test, Pre-Prod.  The columns are consistent in each.

    When my test starts, I check which environment the test is being run against.  I then set a var to indicate the sheet of interest and use that var in the data source step.

    Here's an example flow....

     

    In Get Environment groovy, another if-else based on the currently active environment.

     

    log.info("Checking the environment");
    
    def activeEnvironment = testRunner.testCase.testSuite.project.activeEnvironment.getName();
    def sheetToUse = "";
    
    
    if (activeEnvironment.contains( "Development")) {
    	sheetToUse = "DevSheet";
    } else if (activeEnvironment.contains("Test") ){
    	sheetToUse = "TestSheet";
    } else if (activeEnvironment.contains("Pre-Production") ){
    	sheetToUse = "PreProductionSheet";	
    } else {
    	sheetToUse = "";
    }
    
    log.info("Currently Selected Environment :- ${activeEnvironment}.  Select sheet :- ${sheetToUse}.");
    
    return sheetToUse;

     

     

    Then, in the datasource step, get the result of the groovy script to define the sheet of interest.

     

    Back to your example, you just need to figure some way of detecting when the test is being run through CI.