class_test 1.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 51

class_test #

Allows structuring flutter tests using classes instead of functions to simplify polymorphism testing.

This library is just a simple OO wrapper on top of the original flutter test package.

Getting Started #

Lets imagine you need to test following classes:

abstract class Animal {
  String get kingdom;
  String get order;
  String get family;
}

class Dog implements Animal {
  String get kingdom => 'Animalia';
  String get order => 'Carnivora';
  String get family => 'Canidae';
}

class Cat implements Animal {
  String get kingdom => 'Animalia';
  String get order => 'Carnivora';
  String get family => 'Felidae';
}

Normally you would have to write identical tests both for a Dog and a Cat. Doing this with low code duplication using flutter's default test package might become tricky pretty quickly due to the usage of functions instead of classes.

But worry not: let's define our tests as classes.

First, lets create a test that would work both for a Dog and a Cat:

abstract class AnimalTest extends Test {
  Animal animal;
  
  AnimalTest(this.animal, String name): super(name);
  
  @override
  void declareTests() {
    declareTest('belongs to Animalia kingdom', () {
      expect(animal.kingdom, 'Animalia');
    });
    declareTest('belongs to Carnivora order', () {
      expect(animal.order, 'Carnivora');
    });
  }
}

Now, lets create tests for a Dog and a Cat with parts of those tests that are actually different:

class DogTest extends AnimalTest {
  DogTest(): super(Dog(), 'Dog');
  
  @override
  void declareTests() {
    super.declareTests();
    declareTest('belongs to Canidae family', () {
      expect(animal.family, 'Canidae');
    });
  }
}

class CatTest extends AnimalTest {
  CatTest(): super(Cat(), 'Cat');
  
  @override
  void declareTests() {
    super.declareTests();
    declareTest('belongs to Felidae family', () {
      expect(animal.family, 'Felidae');
    });
  }
}

In order for this two tests to run, lets include them in the main function:

void main() {
  DogTest().run();
  CatTest().run();
}

Setup and teardown #

To specify setUp* and tearDown* method bodies, you can simply override corresponding methods of the Test class.

[1.0.1] - Add Apache License 2.0

[1.0.0] - First release of class_test

example/example.dart

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

abstract class Animal {
  String get kingdom;
  String get order;
  String get family;
}

class Dog implements Animal {
  String get kingdom => 'Animalia';
  String get order => 'Carnivora';
  String get family => 'Canidae';
}

class Cat implements Animal {
  String get kingdom => 'Animalia';
  String get order => 'Carnivora';
  String get family => 'Felidae';
}



abstract class AnimalTest extends Test {
  Animal animal;

  AnimalTest(this.animal, String name): super(name);

  @override
  void declareTests() {
    declareTest('belongs to Animalia kingdom', () {
      expect(animal.kingdom, 'Animalia');
    });
    declareTest('belongs to Carnivora order', () {
      expect(animal.order, 'Carnivora');
    });
  }
}

class DogTest extends AnimalTest {
  DogTest(): super(Dog(), 'Dog');

  @override
  void declareTests() {
    super.declareTests();
    declareTest('belongs to Canidae family', () {
      expect(animal.family, 'Canidae');
    });
  }
}

class CatTest extends AnimalTest {
  CatTest(): super(Cat(), 'Cat');

  @override
  void declareTests() {
    super.declareTests();
    declareTest('belongs to Felidae family', () {
      expect(animal.family, 'Felidae');
    });
  }
}

void main() {
  DogTest().run();
  CatTest().run();
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  class_test: ^1.0.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:class_test/class_test.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
2
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
51
Learn more about scoring.

We analyzed this package on Dec 4, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.6

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
flutter_test 0.0.0
Transitive dependencies
async 2.3.0 2.4.0
boolean_selector 1.0.5
charcode 1.1.2
collection 1.14.11 1.14.12
matcher 0.12.5 0.12.6
meta 1.1.7 1.1.8
path 1.6.4
pedantic 1.8.0+1
quiver 2.0.5 2.1.2+1
sky_engine 0.0.99
source_span 1.5.5
stack_trace 1.9.3
stream_channel 2.0.0
string_scanner 1.0.5
term_glyph 1.1.0
test_api 0.2.5 0.2.11
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
mockito ^4.1.1