Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test a private function, in Dart?

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?

like image 762
Freewind Avatar asked Feb 09 '14 09:02

Freewind


People also ask

How do you test private methods in flutter?

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.

Can you test private methods?

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.

How do you make a dart function private?

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.

How to make a data member of a class private in Dart?

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.

What are UnitTests in Dart testing?

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.

What is the difference between public and private in Dart?

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 _)

What is a function in Dart programming?

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.


1 Answers

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.

like image 57
Dan Field Avatar answered Sep 19 '22 15:09

Dan Field