AI-Powered Flutter Unit Testing: Write Unite Testing with AI with a single Line of Code in minutes

AI-Powered Flutter Unit Testing: Write Unite Testing with AI with a single Line of Code in minutes

As I was browsing the internet, I came across something fascinating called CommandDash . It's like a super-smart tool for testing in Flutter apps. Imagine being able to create tests with just one line of code and getting it done really quickly, in just a few minutes! In this article, we'll take a closer look at this amazing AI-powered Flutter testing tool and see how it can make testing your apps super easy. Let's dive in and explore how WellTested AI is changing the way we test our code.

Before we delve deeper into WellTested AI and its game-changing features, let's first understand what a unit test is in the context of Flutter. In simple terms, a unit test is like a detective for your code. It checks small, individual parts (or units) of your app's functionality to ensure they work correctly. Imagine you have a function that calculates the total price of items in a shopping cart. A unit test for this function would verify that it correctly adds up the prices and returns the expected result. These tests help you catch and fix errors early in the development process, ensuring your app works smoothly and reliably. Now, let's see how WellTested AI can simplify this crucial testing process for Flutter apps.

WellTested serves as the ultimate testing autopilot for Flutter developers, aiming to simplify the testing process seamlessly. In this guide, we'll walk you through the essential steps to get started with WellTested AI:

1. Generate Your API Key

To kickstart your journey with WellTested, begin by creating an account, and you'll receive a unique API key. This key is your access pass to harness the power of AI-driven testing.

2. Set Up Your .env File

In the root directory of your Flutter project, create a .env file. This step is crucial for securely connecting your project to WellTested. Inside the .env file, insert the API key you received via email like this:

WELLTESTED_API=YOUR_API_KEY        

3. Add Dependencies

To make the magic happen, you'll need to integrate the welltested_annotation package into your project's pubspec.yaml file. Additionally, you can include other optional dependencies for more advanced testing features. Your pubspec.yaml should look something like this:

dependencies:
    welltested_annotation: ^1.0.1
dev_dependencies:
    # for generating mocks 
    mockito:
    build_runner:        

4. Activate the WellTested CLI Globally

To utilize WellTested seamlessly across your projects, activate the WellTested CLI globally using this command:

dart pub global activate welltested        

And that's it! You're now fully equipped to tap into the potential of WellTested AI for effortless and efficient testing in your Flutter development journey.

Let's Generate a Test for our app with WellTested

Unit Tests: Simplified and Smart

Unit testing is all about verifying that individual units of your code perform as expected. It involves running various scenarios to confirm that functions and methods behave correctly. While essential, this process can become a significant time sink when done manually.

This is where WellTested AI steps in. It automates the generation of unit tests, making the process not only faster but also smarter. With WellTested AI, you can simulate a wide range of scenarios without the tedium of writing test cases by hand.

Annotations: The Heart of WellTested AI

WellTested AI uses annotations to identify which methods in your code need testing. You can apply these annotations to classes, extensions, or even top-level functions. The @Welltested() annotation is your gateway to streamlined testing.

Here's an example:


Unit Tests: Simplified and Smart

Unit testing is all about verifying that individual units of your code perform as expected. It involves running various scenarios to confirm that functions and methods behave correctly. While essential, this process can become a significant time sink when done manually.

This is where WellTested AI steps in. It automates the generation of unit tests, making the process not only faster but also smarter. With WellTested AI, you can simulate a wide range of scenarios without the tedium of writing test cases by hand.

Annotations: The Heart of WellTested AI

WellTested AI uses annotations to identify which methods in your code need testing. You can apply these annotations to classes, extensions, or even top-level functions. The @Welltested() annotation is your gateway to streamlined testing.

Here's an example:

import 'dart:async';

import 'package:get/get.dart';
import 'package:welltested_annotation/welltested_annotation.dart';

import '../../routes/routes.dart';
import '../../utils/constants/config.dart';
import '../../utils/pref_utils.dart';

@Welltested()
class SplashController extends GetxController {
  final count = 0.obs;
  RxBool isLoading = false.obs;

  updateState() {
    isLoading.value = !isLoading.value;
  }

  @override
  void onInit() {
    Timer(const Duration(seconds: 3), () async {
      int showOnboarding = await SharedPref.readInt(Config.konboarding);
      int showLogin = await SharedPref.readInt(Config.kLoggedIn);
      String? phoneNo = await SharedPref.readString(Config.kPhone);
      int? otpVerified = await SharedPref.readInt(Config.otpVerified);

      if (showOnboarding == 0) {
        Get.offAllNamed(onBoardingRoute);
      } else if (showLogin == 1) {
        if (phoneNo?.isNotEmpty == true) {
          if (otpVerified != 1) {
            Get.offAllNamed(verifyRoute);
          } else {
            Get.offAllNamed(navigationHandler);
          }
        } else {
          Get.offAllNamed(otpRoute);
        }
      } else {
        Get.offAllNamed(loginRoute);
      }
    });
    super.onInit();
  }
}
        

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'])        

Generate Tests with WellTested AI

Now that you're armed with the knowledge of how WellTested AI simplifies and enhances your Flutter unit testing, it's time to put it into action. Generating tests with WellTested AI is a breeze, and here's how you can do it:

Step 1: Run the Command

To generate tests for your annotated classes, open your terminal or command prompt and run the following command:

welltested generate unit        

Step 2: Sit Back and Relax

Once you've initiated the command, WellTested AI will work its magic. The duration of the execution can vary, taking anywhere between 1 to 10 minutes depending on the number of methods in your code.

Step 3: Find Your Tests

After a successful run, you'll find the generated tests conveniently located in the /test directory of your project. These tests will cover the methods you've annotated for testing, ensuring thorough and efficient testing of your codebase.

With this streamlined process, you can save valuable time and trust that your code is well-tested and robust. So, go ahead and let WellTested AI handle the heavy lifting of test generation while you focus on building exceptional Flutter apps.

Harness the power of WellTested AI today and elevate your Flutter development experience to new heights! Happy testing!

Jeff Frazier

Sr Mobile Engineer

6 个月

Very promising, but WellTested is now deprecated. So soon? How can we generate keys now?

回复
Rishabh Agrawal

Senior Flutter Developer | Immediate Joiner

1 年

does it requires our code to push to cloud ?

Samyak Jain

?? Fast Web Scraping API @Olostep

1 年

Thanks for sharing about us Shailendrasingh Rajput! Very well-written and a good read for anyone thinking of using Welltested AI :)

要查看或添加评论,请登录

社区洞察

其他会员也浏览了