Ross
Ross

Reputation: 205

How to access Flutter environment variables from tests?

I've been using flutter_dotenv to load environment variables saved in .env throughout the application and it's worked just fine to date. As I'm trying to write tests though, I cannot seem to access these from a test file.

import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  setUp(() async {
    await DotEnv().load();
  });

  test('Incorrect password should be rejected', () {
    var password = DotEnv().env['PASSWORD'];
    // stuff
  });
}

Result of running the test:

Shell: [flutter_dotenv] Load failed: file not found
Shell: [flutter_dotenv] No env values found. Make sure you have called DotEnv.load()

It just can't seem to find the .env file. I even made a copy of .env in the test directory but it didn't recognise that either.

I've tried using Platform.environment instead of flutter_dotenv to access the variable, but that didn't work either, returning null.

Apologies if I'm being silly here, it's my first time writing Flutter tests, but would appreciate advice.

Update:

This is what my pubspec.yaml looks like:

name: //name
description: //description

version: 1.0.0+3

environment:
  sdk: ">=2.1.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^0.1.2
  font_awesome_flutter: ^8.5.0
  flutter_youtube: ^2.0.0
  http: ^0.12.0+4
  flutter_dotenv: ^2.1.0
  google_fonts: ^0.3.7
  photo_view: ^0.9.2
  flutter_page_transition: ^0.1.0

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:
  uses-material-design: true
  assets:
    - images/
    - .env

Upvotes: 11

Views: 9595

Answers (3)

Sanjay Bharwani
Sanjay Bharwani

Reputation: 4759

Below solutions works for me.

import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_test/flutter_test.dart';

void main() async {


  setUp(() {
    dotenv.testLoad(
        fileInput: '''
          PASSWORD=TEST_PASSWORD 
          KEY2=VALUE2
          KEY3=VALUE3
      ''',
      );
  });

  test('Incorrect password should be rejected', () {
    var password = dotenv.env['PASSWORD'];
    // stuff
  });
}

in pubspec.yaml

flutter_dotenv: ^5.1.0

Upvotes: 0

Daniel Jaramillo
Daniel Jaramillo

Reputation: 11

For those having an error of type FileNotFoundError, update flutter_dotenv. From version 5.0.1 there is new method called testLoad() that works as expected because it doesn't use rootBundle to load the file as an asset.

Upvotes: 1

Bobsar0
Bobsar0

Reputation: 271

Add TestWidgetsFlutterBinding.ensureInitialized(); as the first line of your test's main function.

flutter_dotenv is trying to access ServicesBinding mixin which interacts with your app's platform. You need to initialize this binding first before accessing it and the above line of code will ensure this initialization occurs before running your tests.

Your code should look like this:

import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_test/flutter_test.dart';

void main() async {
  TestWidgetsFlutterBinding.ensureInitialized();
  await DotEnv().load();

  setUp(() {
  // anything else you need to setup
  });

  test('Incorrect password should be rejected', () {
    var password = DotEnv().env['PASSWORD'];
    // stuff
  });
}

Upvotes: 18

Related Questions