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 resultant data source list for execution.

 

How do we achieve this? I

  • 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.

     

     

     

5 Replies

  • richie's avatar
    richie
    Community Hero

    Hey sumit0609 ,

     

    Im responding cos no one else has yet but Im not best placed to help with this one.

     

    I havent actually used tags before - I've just played around with them to familiarise myself with the functionality, however I dont believe tagging will work as its not granular enough for specific records within an individual datasource file.  Tagging works at TestSuite and TestCase level, it doesn't go down to teststep or even records within a teststep level so youre gonna need some alternative.

     

    I tried considering looking at the datasource (file type) to try and find an approach but without some groovy I just cant see how this might work.

     

    If you split out the content of the .csv into individual files .csv files (using the datasource (directory type)) one record (scraped from your original .csv) for each individual .csv, that might make things a little easier, then parameterize your 'Directory' editable field and then use a properties file update (which executes before your automation run) changing the underlying value of the parameter so that only the files you want executed (equivalent to a specific tag value you were thinking about), then perhaps you could get away with this approach without relying on any groovy at all.

     

    If you dont want to split out the single .csv into individual record file .csvs, then as far as I can tell, youre gonna have to rely on groovy - one of the ways I can see this working is to have an additional field (say 'tag value') associated to each individual record in your .csv which equates to your 'tag values' and then have your groovy picking up the relevant records from your .csv based on the the value populating this 'tag value' field.  This isn't necessarily the best approach but its a possible but even then, I've always avoided having a single datafile as my source - Ive always split out my records into individual files to allow greater control. (using the datasource directory type).

     

    My groovy skills are poor - I might be able to get something like this working, but one of the better coders on the forum (nmrao or  ChrisA  or HimanshuTayal or groovyguy) are much better placed to help you out.

     

    Either way - I'd seriously consider moving to the directory datasource option anyway - as I said above, personally Ive always found it a lot easier to manipulate to get what I need rather than relying on one big datasource file and it 'might' allow you to avoid having to use groovy altogether - especially if you dont have any groovy skills.

     

    ta

     

    rich

    • ChrisA's avatar
      ChrisA
      Contributor

      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.

       

       

       

      • richie's avatar
        richie
        Community Hero

        ChrisA 

         

        this is a cracking explanation - really deserves some kudos fella - you've laid it all out so well - highlighting the shortcomings in my own posts when I try and be clear, structured and concise! 

         

        rich