Say I defined a private function in a dart file hello.dart
:
_hello() { return "world"; }
I want to test it in another file mytest.dart
:
library mytest; import 'dart:unittest/unittest.dart'; main() { test('test private functions', () { expect(_hello(), equals("world")); } }
But unfortunately, the test code can't be compiled. But I do need to test that private _hello
function. Is there any solution?
Anyway, I've found a workaround: for each private function that I want to test ( eg. _foo(), I also define a public function foo(){ _foo(); }. Then I place all the public version of such functions in the same area of the file and can easily comment them out for release code.
The short answer is that you shouldn't test private methods directly, but only their effects on the public methods that call them. Unit tests are clients of the object under test, much like the other classes in the code that are dependent on the object.
In Java, we can use public , protected , and private keywords to control the access scope for a property or method. However, Dart doesn't provide that kind of keywords. Instead, you can use _ (underscore) at the start of the name to make a data member of a class becomes private.
Instead, you can use _ (underscore) at the start of the name to make a data member of a class becomes private. In Dart, the privacy is at library level rather than class level. It means other classes and functions in the same library still have the access.
Testing terminology varies, but these are the terms and concepts that you are likely to encounter when using Dart technologies: Unittests focus on verifying the smallest piece of testable software, such as a function, method, or class. Your test suites should have more unit tests than other kinds of tests.
In Dart, the privacy is at library level rather than class level. It means other classes and functions in the same library still have the access. So, a data member is either public (if not preceded by _) or private (if preceded by _)
Dart Programming - Functions. Functions are the building blocks of readable, maintainable, and reusable code. A function is a set of statements to perform a specific task. Functions organize the program into logical blocks of code. Once defined, functions may be called to access code. This makes the code reusable.
While I agree that private methods/classes shouldn't be part of your tests, the meta package does provide an @visibleForTesting
attribute, and the analyzer will give you a warning if you attempt to use the member outside of its original library or a test. You can use it like this:
import 'package:meta/meta.dart'; @visibleForTesting String hello() { return "world"; }
Your tests will now be able to use it without error or warning, but if someone else tries to use it they'll get a warning.
Again, as to the wisdom of doing this is another question - usually if it's something worth testing, it's something that's worth being public (or it'll get tested through your public interfaces and that's what really matters anyway). At the same time, you might just want to have rigorous tests or test driven principles even for your private methods/classes so - Dart lets you this way.
Edit to add: If you're developing a library and your file with @visibleForTesting
will be exported, you are essentially adding public API. Someone can consume that with the analyzer turned off (or just ignore the warning), and if you remove it later you may break them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With