Use data.csv.table to read data as TableData from CSV file. Passed path is either relative based on working dir or absolute file path. Or it can be a resource class path. A B C 1 2 3 a b c Groovy TableData table = data.csv.table("data/table.csv") Check data/import-and-dependencies Import And Dependencies for prerequisites. Java TableData table = data.csv.table("data/table.csv"); Check data/import-and-dependencies Import And Dependencies for prerequisites. read methods produce additional report to help with tests investigation > reading csv from file or resource data/table.csv . read 3 lines of csv from file /home/runner/work/webtau/webtau/webtau-feature-testing/examples/data/table.csv (1ms)
By default, data.csv treats numeric values as strings and does not distinct between strings and dates. Pass valueConverter function to convert values according to your business logic. Groovy def valueConverter = { columnName, value -> columnName == "date" ? LocalDate.parse(value): value } def table = data.csv.table("data/with-dates.csv", valueConverter) table.row(0).date.should == LocalDate.of(2022, 11, 26) table.row(0).date.class.canonicalName.should == "java.time.LocalDate" Java DataCsvValueConverter valueConverter = (columnName, value) -> columnName.equals("date") ? LocalDate.parse(value): value; TableData table = data.csv.table("data/with-dates.csv", valueConverter); actual(table.row(0).get("date")).should(equal(LocalDate.of(2022, 11, 26))); actual(table.row(0).get("date").getClass().getCanonicalName()).should(equal("java.time.LocalDate")); If you only need to deal with numbers, use tableAutoConverted variant that automatically convert numeric values into Number Groovy def table = data.csv.tableAutoConverted("data/table.csv") table.row(0).B.should == 2 table.row(0).B.class.canonicalName.should == "java.lang.Long" Java TableData table = data.csv.tableAutoConverted("data/table.csv"); actual(table.row(0).get("B")).should(equal(2)); actual(table.row(0).get("B").getClass().getCanonicalName()).should(equal("java.lang.Long"));
A B C 1 2 3 a b c Use data.csv.listOfMaps to read data as java.util.List of java.util.Map from CSV file. Passed path is either relative based on working dir or absolute file path. Or it can be a resource class path. Groovy def list = data.csv.listOfMaps("data/table.csv") list.get(0).B.should == "2" list.get(0).B.class.canonicalName.should == "java.lang.String" Java List<Map<String, String>> list = data.csv.listOfMaps("data/table.csv"); actual(list.get(0).get("B")).should(equal("2")); actual(list.get(0).get("B").getClass().getCanonicalName()).should(equal("java.lang.String")); Note: by default numeric values are read as strings, to auto convert numeric values to actual numbers use tableAutoConverted method Use data.csv.listOfMapsAutoConverted to read data as java.util.List of java.util.Map from CSV file. Numeric values become values of Numeric type instead of String type. Passed path is either relative based on working dir or absolute file path. Or it can be a resource class path. Groovy def list = data.csv.listOfMapsAutoConverted("data/table.csv") list.get(0).B.should == 2 list.get(0).B.class.canonicalName.should == "java.lang.Long" Java List<Map<String, ?>> list = data.csv.listOfMapsAutoConverted("data/table.csv"); actual(list.get(0).get("B")).should(equal(2)); actual(list.get(0).get("B").getClass().getCanonicalName()).should(equal("java.lang.Long"));
1, 2, 3 a, b, c Use data.csv.listOfMapsAutoConverted(header, path) to read data as java.util.List of java.util.Map from CSV file. Header will be taken from first parameter and first row of CSV file will not be treated as header. Numeric values become values of Numeric type instead of String type. Passed path is either relative based on working dir or absolute file path. Or it can be a resource class path. Groovy def list = data.csv.listOfMapsAutoConverted(["C1", "C2", "C3"], "data/table-no-header.csv") list.get(0).C2.should == 2 list.get(0).C2.class.canonicalName.should == "java.lang.Long" Java List<Map<String, ?>> list = data.csv.listOfMapsAutoConverted(list("C1", "C2", "C3"), "data/table-no-header.csv"); actual(list.get(0).get("C2")).should(equal(2)); actual(list.get(0).get("C2").getClass().getCanonicalName()).should(equal("java.lang.Long"));
Use data.csv.write to write data to CSV file. Groovy def list = [ ["colA": 1, "colB": "R1"], ["colA": 2, "colB": "R2"]] def path = data.csv.write("generated/from-list-maps.csv", list) Java List<Map<String, ?>> list = list( map("colA", 1, "colB", "R1"), map("colA", 2, "colB", "R2")); Path path = data.csv.write("generated/from-list-maps.csv", list); write methods produce additional information that helps with tests investigation > writing csv to file generated/from-list-maps.csv . wrote 4 lines to csv /home/runner/work/webtau/webtau/webtau-feature-testing/examples/generated/from-list-maps.csv (1ms) colA,colB 1,R1 2,R2
Groovy TableData table = ["id" | "value"] { ________________ "id1" | "value1" "id2" | "value2" } def path = data.csv.write("generated/from-table-data.csv", table) Java TableData csvTable = table("id" , "value", ________________, "id1" , "value1", "id2" , "value2"); Path path = data.csv.write("generated/from-table-data.csv", csvTable); write methods produce additional information that helps with tests investigation > writing csv to file generated/from-table-data.csv . wrote 4 lines to csv /home/runner/work/webtau/webtau/webtau-feature-testing/examples/generated/from-table-data.csv (0ms) id,value id1,value1 id2,value2