Coder Social home page Coder Social logo

whiteshadow1234 / flutter_widget_and_unit_test Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 279 KB

An example of how to do unit test and widget test in flutter

Kotlin 0.24% Swift 2.31% Objective-C 0.08% Dart 12.01% CMake 37.22% C++ 41.68% C 2.84% HTML 3.62%
flutter-app unit-testing widget-testing

flutter_widget_and_unit_test's Introduction

uTest

Steps

  1. Add unit_test.dart in the test folder

  2. Adjust some code from this script and paste it in unit_test.dart:

import 'package:flutter_test/flutter_test.dart';
import 'package:utest/counter.dart';

import 'package:utest/main.dart';

void main() {
  test('Counter value should be incremented', () {
    // setup the counter class
    final counter = Counter();
    //expect(counter.value, 0);
    
    // do the testing part
    counter.increment();
    
    // check if the result is expected
    expect(counter.value, 1);
  });
}
  1. Create a counter.dart in the same directory where main.dart is and build up your class:
class Counter {
  int value = 0;
  void increment() {
    value++;
  }
}
  1. Revise the code in main.dart with the class you just created:
//...

class _MyHomePageState extends State<MyHomePage> {
  Counter counter = Counter();

  // void _incrementCounter() {
  //   setState(() {
  //     // This call to setState tells the Flutter framework that something has
  //     // changed in this State, which causes it to rerun the build method below
  //     // so that the display can reflect the updated values. If we changed
  //     // _counter without calling setState(), then the build method would not be
  //     // called again, and so nothing would appear to happen.
  //     _counter++;
  //   });
  // }

  @override
  Widget build(BuildContext context) {
      //...
//...
        children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '${counter.value}',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
//...
//...
    floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            counter.increment();
          });
        },
//...
  1. Next, press the testing button in the left side in VScode and press Debug Test button
testing button Debug Test button

Result

flutter_widget_and_unit_test's People

Contributors

whiteshadow1234 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.