Giter Club home page Giter Club logo

welltested's Introduction

welltested

Welltested is an AI testing autopilot for Flutter developers add and maintain intelligent tests coverage and deliver stable welltested apps to users.

Generate and Configure API Key

🗝️ Generate API Key

Welltested is completely free for individual developers. To create an account, please sign up here.

⚙️ Configure API Key

Create a .env file in root of your project, and add your key received in the email.

WELLTESTED_API=YOUR_API_KEY

We uses dotenv to retrive API Key. Once .env file is created, please declare it as an asset in pubspec.yaml to make is accessible to the package.

assets:
  - .env

Let's create tests

Add the @Welltested() annotation to the class whose methods are to be tested.

@Welltested()
class Auth {
    Future<void> logInUser() {...}
    Future<void> logOutUser() {...}
}

In the above case, it will generate unit tests for logInUser and logOutUser methods of Auth class.

Exclude Methods

By default, all methods in an annotated class are included. To exclude any method from testing, add their name to the excludedMethods list in the Welltested annotation.

@Welltested(excludedMethods: ['logOutUser'])
class Auth {
    Future<void> logInUser() {...}
    Future<void> logOutUser() {...}
}

In the above case, it will not create unit tests for logOutUser method.

Custom Testcases

To specify custom testcases for any method, use @Testcases() annotation.

@Welltested()
class Auth {
    @Testcases(['Throws exception when email is empty'])
    Future<User> logInUser() {...}
}

Generate Tests

To generate unit tests for annotated classes, please run the following command:

dart run welltested:ai build

Saving Tests

At times, tests generated by welltested might have syntax issues. When encountering such issues, please makes the fixes and run the below command to save your changes. We remember your fixes and don't repeat the mistake.

dart run welltested:save build

USAGE WIKI

To read detailed setup instructions, code guidelines and supported platform infromation, read our Usage Wiki.

NOTE

  1. The minimum supported Flutter SDK version is 3.0.0 with Dart SDK 2.17.0. Any prior projects need to be upgraded for welltested to work.

For complete Example please check example project.

welltested's People

Contributors

samyakkkk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

welltested's Issues

Update usage instructions

Instructions are unclear and confusing. According to current Instructions from README it says to use this command welltested generate build but it throws error saying Could not find a subcommand named "build" for "welltested generate". and when help command is used it says generate, build these two sub commands are available but when build sub command is executed it says only unit sub command is available

pub_semver-2.1.1 FallThroughError()

Errors Logs:

Building package executable... 
Failed to build welltested:ai:
../../../../.pub-cache/hosted/pub.dev/pub_semver-2.1.1/lib/src/version_constraint.dart:96:13: Error: Method not found: 'FallThroughError'.
      throw FallThroughError();
            ^^^^^^^^^^^^^^^^
Failed to build welltested:ai:
../../../../.pub-cache/hosted/pub.dev/pub_semver-2.1.1/lib/src/version_constraint.dart:96:13: Error: Method not found: 'FallThroughError'.
      throw FallThroughError();

cli commands can't access private members

The first time I ran this command it worked fine and gave me an output after 30 seconds or so. Later executions just show nothing for hours just Analyzing testability for methodName and I don't see any errors anywhere.

Edit: I tried making some bloc members public and it seems to be able to read the events but I really don't want to do that. As shown over here in the AppBloc, https://bloclibrary.dev/#/flutterfirebaselogintutorial?id=bloc the onEvents are supposed to be private not public and I believe that's a good coding practice even in the repository layers. I hope you can update the package to be able to support private members because it's seems like a cool idea.

Generated test assigns the 'null' value to non-nullable value

I have a model 'product.dart'. I wanted to test that product using welltested.

here is the model code.

import 'package:json_annotation/json_annotation.dart';
import 'package:welltested/welltested.dart';

part 'product.g.dart';

@JsonSerializable()
@Welltested()
class Product {
  final int id;
  final String title;
  final num price;
  final String description;
  final String image;
  final String category;
  final Rating rating;

  Product(
      {required this.category,
      required this.description,
      required this.id,
      required this.image,
      required this.price,
      required this.title,
      required this.rating});

  factory Product.fromJson(Map<String, dynamic> json) =>
      _$ProductFromJson(json);

  Map<String, dynamic> toJson() => _$ProductToJson(this);
}

@JsonSerializable()
class Rating {
  final num rating;
  final int count;

  Rating({
    required this.count,
    required this.rating,
  });

  factory Rating.fromJson(Map<String, dynamic> json) => _$RatingFromJson(json);
  Map<String, dynamic> toJson() => _$RatingToJson(this);
}

Two test files were generated by welltested ai.

Screen Shot 2023-06-23 at 10 38 52

Here is the code for each of the files :

toJson.welltested_test.dart

import 'toJson.welltested_test.mocks.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:api_integrations/product.dart';

// Generate mocks for Rating class
@GenerateMocks([Rating])
void main() {
  group('Product toJson method', () {
    test('should return a valid JSON map', () {
      // Arrange
      final ratingMock = MockRating();
      when(ratingMock.rating).thenReturn(4.5);
      when(ratingMock.count).thenReturn(100);
      final product = Product(
        id: 1,
        title: 'Test Product',
        price: 9.99,
        description: 'This is a test product',
        image: 'test_image.jpg',
        category: 'Test Category',
        rating: ratingMock,
      );

      // Act
      final result = product.toJson();

      // Assert
      expect(result, isA<Map<String, dynamic>>());
      expect(result['id'], equals(1));
      expect(result['title'], equals('Test Product'));
      expect(result['price'], equals(9.99));
      expect(result['description'], equals('This is a test product'));
      expect(result['image'], equals('test_image.jpg'));
      expect(result['category'], equals('Test Category'));
      expect(result['rating'], isA<Map<String, dynamic>>());
      expect(result['rating']['rating'], equals(4.5));
      expect(result['rating']['count'], equals(100));
    });

    test('should return a JSON map with null values', () {
      // Arrange
      final product = Product(
        id: 1,
        title: 'Test Product',
        price: null,
        description: null,
        image: null,
        category: null,
        rating: null,
      );

      // Act
      final result = product.toJson();

      // Assert
      expect(result, isA<Map<String, dynamic>>());
      expect(result['id'], equals(1));
      expect(result['title'], equals('Test Product'));
      expect(result['price'], isNull);
      expect(result['description'], isNull);
      expect(result['image'], isNull);
      expect(result['category'], isNull);
      expect(result['rating'], isNull);
    });

    test('should throw an exception if id is null', () {
      // Arrange
      final ratingMock = MockRating();
      when(ratingMock.rating).thenReturn(4.5);
      when(ratingMock.count).thenReturn(100);
      final product = Product(
        id: null,
        title: 'Test Product',
        price: 9.99,
        description: 'This is a test product',
        image: 'test_image.jpg',
        category: 'Test Category',
        rating: ratingMock,
      );

      // Act & Assert
      expect(
          () => product.toJson(), throwsA(isA<JsonUnsupportedObjectError>()));
    });
  });
}

toJson.welltested_test.mocks.dart

// Mocks generated by Mockito 5.4.2 from annotations
// in api_integrations/test/product/0.wt.dart.
// Do not manually edit this file.

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'package:api_integrations/product.dart' as _i2;
import 'package:mockito/mockito.dart' as _i1;

// ignore_for_file: type=lint
// ignore_for_file: avoid_redundant_argument_values
// ignore_for_file: avoid_setters_without_getters
// ignore_for_file: comment_references
// ignore_for_file: implementation_imports
// ignore_for_file: invalid_use_of_visible_for_testing_member
// ignore_for_file: prefer_const_constructors
// ignore_for_file: unnecessary_parenthesis
// ignore_for_file: camel_case_types
// ignore_for_file: subtype_of_sealed_class

/// A class which mocks [Rating].
///
/// See the documentation for Mockito's code generation for more information.
class MockRating extends _i1.Mock implements _i2.Rating {
  MockRating() {
    _i1.throwOnMissingStub(this);
  }

  @override
  num get rating => (super.noSuchMethod(
        Invocation.getter(#rating),
        returnValue: 0,
      ) as num);
  @override
  int get count => (super.noSuchMethod(
        Invocation.getter(#count),
        returnValue: 0,
      ) as int);
  @override
  Map<String, dynamic> toJson() => (super.noSuchMethod(
        Invocation.method(
          #toJson,
          [],
        ),
        returnValue: <String, dynamic>{},
      ) as Map<String, dynamic>);
}

My pubspec.yaml file

name: api_integrations
description: A new Flutter project.
publish_to: 'none'
version: 0.1.0

environment:
  sdk: '>=3.0.0-408.0.dev <4.0.0'

dependencies:
  flutter:
    sdk: flutter
  http: ^0.13.6
  json_serializable: ^6.6.2
  mockito: ^5.4.2
  welltested: ^1.0.4

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^2.0.0
  build_runner:

flutter:
  uses-material-design: true


Thank you !

blocTest

Hi 👋 Very cool library, I'm wondering if there will be a possibility to generate bloc_test (maybe even with mocktail)?

And also did you consider adding dart fix --apply command after generating the files? It cleans it all up pretty nicely 🙂

Sign-up seems to be broken

Hi there,

We'd like to try out Welltested, but when trying to sign-up for the Free Plan, i can't create an account.

The console shows the following error:
Bildschirmfoto 2024-03-08 um 14 20 27

Could you help me there?

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.