File can be read from the directory relative to the dart script file, simply as var file = new File('./fixture/contacts.json')
.
However the file cannot be read flutter test running inside IDE.
Loading as resource (it is not, since I do not want to bundle the test data file in the app) also does not work in test.
What is a good way to read file in flutter (for both command line test and IDE test)?
Running flutter test
is very fast. However testing inside Intellij IDE is ver slow, but it can set debug breakpoint and step into and view variables. So both testing is very useful.
Test-Driven Development cycle TDD is also called the Red-Green-Refactor process regarding its iterative process, which is consisted of 3 following steps: Write a test that fails (Red) Write a code to make a test pass (Green) Refactor your code to obtain high code quality (Refactor)
The flutter_test package provides the following tools for testing widgets: The WidgetTester allows building and interacting with widgets in a test environment. The testWidgets() function automatically creates a new WidgetTester for each test case, and is used in place of the normal test() function.
Just had a go at this and it's easier than you might expect.
First, create a folder that lives in the same directory than your tests are. For example, I created a folder called test_resources.
Then, let's say we have the following JSON file for testing purposes.
test_resources/contacts.json
{ "contacts": [ { "id": 1, "name": "Seth Ladd" }, { "id": 2, "name": "Eric Seidel" } ] }
test/load_file_test.dart
We could use it for our test like so:
import 'dart:convert'; import 'dart:io'; import 'package:flutter_test/flutter_test.dart'; void main() { test('Load a file', () async { final file = new File('test_resources/contacts.json'); final json = jsonDecode(await file.readAsString()); final contacts = json['contacts']; final seth = contacts.first; expect(seth['id'], 1); expect(seth['name'], 'Seth Ladd'); final eric = contacts.last; expect(eric['id'], 2); expect(eric['name'], 'Eric Seidel'); }); }
I encountered this same issue today. My tests passed in my IDE (Android Studio) but failed on CI. I found that the IDE wanted a non-relative path but flutter test
wants a relative path. I think this is a bug and I'll be looking for the right place to report it. However, I found a crude work around for the meantime. Basically, I have a function which tries both ways to specify the file path..
import 'dart:convert'; import 'dart:io'; Future<Map<String, dynamic>> parseRawSample(String filePath, [bool relative = true]) async { filePath = relative ? "test_data/src/samples/raw/$filePath" : filePath; String jsonString; try { jsonString = await File(filePath).readAsString(); } catch (e) { jsonString = await File("../" + filePath).readAsString(); } return json.decode(jsonString); }
much of that code is specific to my situation, but I think others could probably adapt this work around for their purposes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With