Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a "correct" folder where to place test resources in dart?

Tags:

directory

dart

I have a simple dart class I am trying to test.
To test it I need to open a txt file, feed the content to an instance of the class and check that the output is correct.

Where do I place this txt file? The txt file is useless outside of testing.

Also, related, how do I acess its directory consistently? I tried placing it in the test folder, but the problem is that:

System.currentDirectory

Returns a different directory if I am running the test on its own or the script calling all the other test dart files on at a time

like image 672
CarrKnight Avatar asked Nov 29 '14 09:11

CarrKnight


3 Answers

I check if System.currentDirectory is the directory containing the pubspec.yaml file, if not I move the current directory upwards until I found the directory containing the pubpsec.yaml file and then continue with the test code.

like image 102
Günter Zöchbauer Avatar answered Oct 29 '22 18:10

Günter Zöchbauer


Looks like package https://pub.dev/packages/resource is also suitable for this now.

like image 20
Alexander Arendar Avatar answered Oct 29 '22 17:10

Alexander Arendar


I have still not found a definitive answer to this question. I've been looking for something similar to the testdata directory in Go and the src/test/resources directory in Java.

I'm using Android studio and have settled on using a test_data.dart file at the top of my test directory. In there I define my test data (mostly JSON) and then import it into my individual tests. This doesn't help if you need to deal with binary files but it has been useful for my JSON data. I'll also inject the JSON language with //language=json so I can open the fragment in a separate window to format.

//language=json
const consolidatedWeatherJson = '''{
  "consolidated_weather": [
    {
      "id": 4907479830888448,
      "weather_state_name": "Showers",
      "weather_state_abbr": "s",
      "wind_direction_compass": "SW",
      "created": "2020-10-26T00:20:01.840132Z",
      "applicable_date": "2020-10-26",
      "min_temp": 7.9399999999999995,
      "max_temp": 13.239999999999998,
      "the_temp": 12.825,
      "wind_speed": 7.876886316914553,
      "wind_direction": 246.17046093256732,
      "air_pressure": 997.0,
      "humidity": 73,
      "visibility": 11.037727173307882,
      "predictability": 73
    }
  ]
}
''';

Using the Alt + Enter key combination will bring up the Edit JSON Fragment option. Selecting that open the fragment in a new editor and any changes made there (formatting for example) will be updated in the fragment.

Not perfect but it solves my issues.

like image 23
Mike Dalrymple Avatar answered Oct 29 '22 18:10

Mike Dalrymple