-3

Im writing a UITest to check if a List of Countries populated in my tableview are displayed in ascending order. I am populating the countries from Countries api

The purpose of my UITest is to check the countries are displayed in alphabetical order, so I pick ten countries at random in ascending order of table index and confirm that the countries are in ascending order too.

let table = app.tables["CountryTable"]
XCTAssertTrue(table.waitForExistence(timeout: kTimeOut))
let cells = table.cells.allElementsBoundByIndex

var indexes = Set<UInt32>()
while indexes.count < 10 {
    indexes.insert(arc4random_uniform(UInt32(cells.count)))
}

let orderedIndexes = Array(indexes).sorted()
let countries = orderedIndexes.map { (index) -> String in
    return cells[Int(index)].staticTexts["Country"].label
}

let orderedCountries = countries.sorted()
XCTAssertEqual(countries, orderedCountries)

In my ViewController i can see that the countries are sorted in ascending order, but my Test is failing.

i will be glad to be educated on how to test this approach appropriately and make the Test pass.

Arasuvel
  • 2,971
  • 1
  • 25
  • 40
BigFire
  • 317
  • 1
  • 4
  • 17
  • 2
    Normally you sort the array before you use it in the UI like a table view and then this isn't really a UI test to verify that the array is sorted. – Joakim Danielson Apr 07 '22 at 09:54

1 Answers1

0

This looks more like a case for a unit test than a UI test. You probably have a module that loads the countries and acts as the data source for the tableview. That module is what you should be testing here.

Here's why your test is probably failing: You have selected the random indices, and ordered them. Then you use this ordered list of indices to fetch the corresponding countries. At this point, the two lists should match, ie orderedIndexes and countries. But then you go and sort the countries list again using countries.sorted(). So obviously orderedCountries and orderedIndexes will be out of sync.

inTheAM
  • 26
  • 2