-
Notifications
You must be signed in to change notification settings - Fork 28.6k
Flutter test coverage will not report untested files #27997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
i searched the same question on stack overflow: but none answer it. |
I have found a temporary workaround for this issue. If you create a "fileLoader_test.dart" file like this:
then "flutter test --coverage" will recognize all files specified in the "lib.dart" files. |
yep @BigBl4ckW0lf I got this, but i must import all files in my project. |
If a file is not reachable by the entrypoint that executes coverage, then because the VM has never loaded the library it doesn't return any coverage information for us. The tester process could walk the filesystem and insert the remaining file URIs into the coverage report - it's not clear that would always be meaningful information or the right thing to do. |
I'm using @BigBl4ckW0lf's idea but didn't want to manually add them in case I forget. I've got a CI step which creates the file before running the coverage using this script #!/bin/sh
file=test/coverage_helper_test.dart
echo "// Helper file to make coverage work for all dart files\n" > $file
echo "// ignore_for_file: unused_import" >> $file
find lib -name '*.dart' | cut -c4- | awk -v package=$1 '{printf "import '\''package:%s%s'\'';\n", package, $1}' >> $file
echo "\nvoid main(){}" >> $file Just call it with your package name as an argument and it'll generate a test including all dart files in the lib folder |
Here is a version which exclude .g.dart files #!/bin/sh
file=test/coverage_helper_test.dart
echo "// Helper file to make coverage work for all dart files\n" > $file
echo "// ignore_for_file: unused_import" >> $file
find lib -not -name '*.g.dart' -and -name '*.dart' | cut -c4- | awk -v package=$1 '{printf "import '\''package:%s%s'\'';\n", package, $1}' >> $file
echo "void main(){}" >> $file |
were do I have to add thuis |
any way to avoid part libraries? Or just ignore the warning? |
Hi, #!/bin/sh
file=test/coverage_helper_test.dart
printf "// Helper file to make coverage work for all dart files\n" > $file
printf "// **************************************************************************\n" >> $file
printf "// Because of this: https://github.com/flutter/flutter/issues/27997#issue-410722816\n" >> $file
printf "// DO NOT EDIT THIS FILE USE: sh scripts/import_files_coverage.sh YOUR_PACKAGE_NAME\n" >> $file
printf "// **************************************************************************\n" >> $file
printf "\n" >> $file
printf "// ignore_for_file: unused_import\n" >> $file
find lib -type f \( -iname "*.dart" ! -iname "*.g.dart" ! -iname "*.freezed.dart" ! -iname "generated_plugin_registrant.dart" \) | cut -c4- | awk -v package="$1" '{printf "import '\''package:%s%s'\'';\n", package, $1}' >> $file
printf "\nvoid main(){}" >> $file Don't forget to change YOUR_PACKAGE_NAME by your package. And you can also ignore this file on your .gitignore.
|
@vrnvorona do you found any way to avoid part libraries from |
Didn't try anything yet sadly, so no. |
Add a |
Its been quite some time and yet there seems to be little progress here, @jonahwilliams anything particular that's blocking this issue? Is it slated to be fixed in the next dart release? |
To add to the workarounds previously provided, I wrote one up in dart that also gets rid of files with a Just replace the
|
Just came across this issue as well and I found there is also a flutter package that does the exact same thing: We added it as a dev dependency to our pubspec.yaml and just call |
Hi everyone I used a simple workaround on my side. In the // ignore: unused_import
import 'package:example/main.dart';
void main() {
// Fake test in order to make each file reachable by the coverage
} Every file used by the app will be scanned by the coverage tool. 🎉 Enjoy 🎉 |
Thanks for this. Just a note, if you're using Windows (boo hiss, I know!) you need to replace \ with / for the generated imports, e.g.:
|
I've used the approach suggested above in the comments and it's worked great for most of my use cases, however I was wondering if there was a way to perform this action (include untested files in the coverage calculation) when the package uses the |
I think the better approach is to really test the main. By default any new Flutter app already includes a Widget test for the counter app. The workaround I suggested can provide metrics about uncovered sources but a cleaner way to do should be to create a simple I am wondering if we should include this test by default in the |
What if there is no main? I have a project that is just a collection of many widgets and some logic used by different apps. Flutter test will not pick them up, therefore the coverage is off by a lot. |
The current coverage engine works fine. The real problem is files not included in the coverage because no Dart file import it. What if we create a Dart CLI package in order to add missing files in the lcov file juste after coverage execution ? The process could be
Both steps could be included in a single CLI command The most complicated part will be to scan Dart sources in order to get an lcov format output. This way, real coverage value could be reported. I'll try to create a package and see if it works or not |
Hi Can someone give a try on https://pub.dev/packages/discover ? I think it's a good start. Open any Flutter projet and run:
Discover will
Let me know how I can improve it. @CBeening let me know if it works on your Flutter widgets package. |
I have always used a solution based on this comment by pdblasi which has given good results, basically generating a huge import file including all relevant project files (except generated ones), but having to update this file all the time is sub-optimal, so looking forward to this new package, which is way cleaner. I did a few tests with a small projects and the only thing I noticed so far is, that that my barrel files (which only contain export 'file_name.dart' lines are being taken into account in the coverage report, while in the normal coverage reports, they are ignored/absent. This was the only thing I found so far, so looking good! EDIT: To not spam this thread... @PiotrFLEURY All these lines contribute to a lower coverage percentage when using discover, compared to the regular way. Another thing I noticed is that when a file is missing, "discover scan" will fix the coverage AND open the lcov-report. However when all files are already referenced, I get a message "All Dart files are listed in the coverage file." and the lcov-report needs to be manually generated. It would be nice (either through a setting or by default) that the result (and side effects)of "discover scan" is always the same: either always output the result of scan or generate the report and open it. This way it's easy to include the command in a shell script. Normally I run a shell script by typing dcov which will execute the following. It would be nice if "discover scan" could either fit in the script or replace it completely. dart pub global activate coverage; Either way, the package is already really useable, thanks! |
Thanks for testing @tekneurt Barrel files are now ignored by default. Discover package version I guess I should manage a EDIT: Version 0.2.0 has been publish including EDIT: @tekneurt version 0.3.0 has just been published containing better code filtering. Last edit here to not spam this thread about this package. Let's continue on the discover repo. If anything need to be fixed or improved please open an issue. Thanks for testing again ! Happy to help the community with my little contributions. |
I run the
flutter test --coverage
as followsand here are my project's files;
the generated
Icov.info
looks like this:why coverage didn't include the untested files?
i know how to show coverage by tools like
genHtml
locv-sumary
I just want to know how to generate the Locv.info that includes untested files in my projects
The text was updated successfully, but these errors were encountered: