original
https://medium.com/@sanketsmasurkar/how-do-we-test-flutter-applications-unit-test-beb6c8d9d5ac
refer to
text
Hello everyone!
Let's first understand why we need to perform automated testing on an application while it is still in development.
Automated testing is a method of writing and running suites of test cases on a software application using an automated tool testing tool.
If we test the application during the development phase, we can save time by using automated tests to detect new bugs that might lead to routine changes.
There are several types of automated tests in Flutter:
- Unit Test Unit Test
- Widget Test Widget Test
- Integration Test
Note: We only discuss unit testing in this article.
What are unit tests and test cases?
Unit tests are used to verify the correctness of a single function, method or class. To test this function, method or class, a test case must be written. Conditions that must be met in order to verify the expected output are called test cases.
Implementation procedure:
In this article, we will run a test case for a login page, which has two fields: email id and password.
Step 1: Make sure the flutter_test
dependency exists in the pubspec yaml file.
dev_dependencies:
flutter_test:
sdk: flutter
Step 2: Create the validator class.
All our validation methods will be written in this class. These methods will be used in our login page (in the email and password textformfield validator parameters) and in the login_unit_test class.
- PasswordTextFormField\_
TextFormField(
validator: Validators.validatePwd,
)
- Validator Class Validator class
Step 3: Create a login_unit_test class in the test directory and write a test case for Login.
Now, we have reached the stage of writing test cases.
void main() {
test('Verify invalid email address', () {
String emailId = 'abc@gmail';
var result = Validators._validateEmailId_(emailId);
expect(result, 'Enter valid Email ID.');
});
test('Verify valid password', () {
String pwd = 'Qwe@12345';
var result = Validators._validatePwd_(pwd);
expect(result, null);
});
}
We use the test function to define test cases. We have to provide two mandatory parameters in this function. The first parameter is a string value, i.e. (a description of a specific case), and the second parameter is a function in which we validate the test case.
We use the expect function to compare the results with expectations. The Expect function also has two required parameters. The first argument is the result returned by the validator function, and the second argument is the comparator or expected result.
The test package provides test and expectation functions.
Let's go back to the previous example.
The first test function is to verify the email address. As you can see, there are two variables: emailId and result _emailId_ After that, the Validator class function returns the value to the result variable.
So some of you may be wondering why I kept the expected result value of the second test case as _null_.
If you continue to step 3, where we created the validator class, you will notice that if the value is not empty or matches a regular expression, we return an empty value. That's why the expected result value is set to null. Make sure the result is correct.
You can also group your test cases together.
group('Multiple test cases for password verification', () {
test('Verify password', () {
String pwd = 'Abc@12345';
var result = Validators._validatePwd_(pwd);
expect(result, null);
});
test('Verify empty password', () {
String pwd = '';
var result = Validators._validatePwd_(pwd);
expect(result, 'Enter Password');
});
});
Step 4: Run the test case.
To run all test cases, just enter the following command in terminal: flutter test test/name_of_your_file.dart
- When all test cases pass, this is the result.
- You can also run a single test case by clicking the play button, I've highlighted this in the image below.
- If the test case fails, below is the result.
Conclusion:
Here's a quick overview of unit testing in Flutter. I hope I have given you enough information to try to unit test in your project.
© Cat Brother
- WeChat ducafecat
- Blogducafecat.tech
- github
- bilibili
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。