The original address: swerdlowben.medium.com/automating-…
Swerdlowben.medium.com/
Published: 23 December 2020 -3 minutes to read
An overview of the
Code reviews can become time-consuming when working on a large team or open source project, especially if you have to read a lot of poorly written code. One way to reduce this burden is to set up automatic code analysis, formatting, and UI testing.
This tutorial shows you how to set up Github Actions to check code quality, formatting, and run UI tests to verify pull requests.
All the code in this repository will be provided here as a template: github.com/theswerd/fl…
A prerequisite for
- A Github version library
- Flutter installation
steps
Create a Flutter App in the root of the Github repository.
flutter create --project-name myapp .
Copy the code
Create a file called Analysis_options.yaml. This file will be used to customize the parser to ignore flutter dependencies installed in the action.
The file should look something like this.
analyzer:
exclude:
- flutter/**
Copy the code
Create a Github action
Making Actions are stored in. Making/workflows folder, so for the test, create a named: making/workflows/tests. The yaml file.
Parts.
Name:
The name is the first part of the Github Actions file, which defines the name of the action in the file.
name: TEST FLUTTER APP
Copy the code
about
The On section defines when to run the action. This action needs to be run every time there is a pull request in order to validate the code.
on:
push:
pull_request:
branches:
- main
Copy the code
The environment
Env defines the global environment variable for the action. To make it easy to update, place the Flutter version in an environment variable.
env:
FLUTTER_VERSION: "1.22.1"
Copy the code
homework
The jobs section defines the jobs to be completed. The dancing test assignment looks something like this.
jobs:
tests:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Install Flutter
run: git clone https://github.com/flutter/flutter.git --depth 1 -b $FLUTTER_VERSION flutter
- name: Add Flutter to Path
run: echo "$GITHUB_WORKSPACE/flutter/bin" >> $GITHUB_PATH
- name: Install Dependencies
run: flutter pub get
- name: Flutter Analyze
run: flutter analyze --no-pub
- name: Check Flutter Formatting
run: flutter format lib/** --set-exit-if-changed
- name: Run Flutter Tests
run: flutter test --no-pub
Copy the code
Break down:
- Checkout Repository checks out the Repository code to the Action
- Get the Flutter code from the Flutter Repository on Github.
- Add a Flutter to the path of a Github Action so that it can use the Flutter CLI.
- Install application dependencies
- Run flutter Analyze to analyze the code and check for errors such as unused imports.
- Run the flutter format lib/** –set-exit-if-changed to check that the code is properly formatted. If the code is badly formatted, an error is thrown.
- Run flutter tests to run tests defined in your application’s test directory.
A concerted effort to
name: TEST FLUTTER APP
on:
push:
pull_request:
branches:
- main
env:
FLUTTER_VERSION: "1.22.1"
jobs:
tests:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Install Flutter
run: git clone https://github.com/flutter/flutter.git --depth 1 -b $FLUTTER_VERSION _flutter
- name: Add Flutter to Path
run: echo "$GITHUB_WORKSPACE/_flutter/bin" >> $GITHUB_PATH
- name: Install Flutter Dependencies
run: flutter pub get
- name: Flutter Analyze
run: flutter analyze --no-pub
- name: Check Flutter Formatting
run: flutter format lib/** --set-exit-if-changed
- name: Run Flutter Tests
run: flutter test --no-pub
Copy the code
Automatic test in action
When someone makes a bad Pull Request.
Poorly written code fails the test
When someone requests code and the code is good.
Well-written code passes tests
The closing
These automated tests can help you maintain high code quality and reduce your work. Hopefully it will help you with your future FLUTTER project.
All the code in this repository will be provided here in template form: github.com/theswerd/fl…
Translation via www.DeepL.com/Translator (free version)