How to setup flutter unit tests on windows - windows

I have an existing flutter application, that I would like to configure with a couple of simple unit tests. I followed instructions from https://codelabs.developers.google.com/codelabs/flutter-app-testing#0 with pointers on how to configure a project with unit tests, and applied those steps to my own project.
After applying those steps flutter test is not behaving like how I would expect, and blatantly just doesn't work. In specific, it yields the following output Connection closed before test suite loaded.
While trying to resolve the error a couple of oddities:
When I ran unit tests (from vscode) I noticed that the application was "paused" on an unhandled exception.
When I looked into the exception, it was about a dependency my actual application relies on. Specifically, about SQLite not being able to find the .so/.dll
When I looked into the callstack, I noticed that it was basically just loading my actual application instead of the unit tests own entry point. Specifically, it was loading all of my app providers.
After I removed that specific exception, it would continue to fail on other parts. For example about unhandled exceptions over missing assets. This is something I could work around with --[no-]test-assets to force the compilation of assets.
The behavior I have witnessed above. I sort of came to a conclusion that it's trying to execute the actual application instead of the tests altogether (hence the various missing dependencies etc). So as a theory, I removed the main function from my main.dart. Running flutter test then fails to compile some autogenerated generated_main.dart, because the entrypoint is missing.
//
// Generated file. Do not edit.
// This file is generated from template in file `flutter_tools/lib/src/flutter_plugins.dart`.
//
// #dart = 2.6
import 'package:liquid/main.dart' as entrypoint;
import 'dart:io'; // flutter_ignore: dart_io_import.
import 'package:path_provider_linux/path_provider_linux.dart';
import 'package:path_provider_windows/path_provider_windows.dart';
... //stripped for brevity
typedef _UnaryFunction = dynamic Function(List<String> args);
typedef _NullaryFunction = dynamic Function();
void main(List<String> args) {
if (entrypoint.main is _UnaryFunction) {
(entrypoint.main as _UnaryFunction)(args);
} else {
(entrypoint.main as _NullaryFunction)();
}
}
From what I observed, most of the issues seem to come from the fact that's choosing to run my actual application instead of the unit test. That first line import 'package:liquid/main.dart' as entrypoint; is basically referencing the application entrypoint.
According to https://github.com/flutter/flutter/issues/8516 there used to be a time where flutter test wasn't supported but it's already been closed. So I assume there are no fundamental broken issues "just because windows platform".
However, it appears that something is not completely right on my setup and I am not really sure what I need to do to get a simple test case working.

Related

Why we need `afterEach(cleanup);`?

This is question about unit test (jest + #testing-library/react)
Hi. I started using #nrwl/react these days.
This is amazing products and I'm excited monorepos project with nx.
Btw, there is afterEach(cleanup); in generated template test file.
This is my sample project.
https://github.com/tyankatsu0105/reproducibility-react-test-nx/blob/master/apps/client/src/app/app.spec.tsx#L7
However react-testing-library doesn't need cleanup when using jest.
https://testing-library.com/docs/react-testing-library/api#cleanup
Please note that this is done automatically if the testing framework you're using supports the afterEach global (like mocha, Jest, and Jasmine). If not, you will need to do manual cleanups after each test.
In fact, I see error when remove afterEach(cleanup); from test files.
Found multiple elements with the text:
thanks!

Unit test does not cover locally imported packages

I am new to golang and trying to understand how i can make this scenario work?
Here is my structure
GOPATH set to /Users/xyz/project
/Users/xyz/project/src/main.go // import calculator and call SUM with two integeres
/Users/xyz/project/src/main_test.go // test function
/Users/xyz/project/src/calculator/sum.go // SUM function (add two integers)
i have a main go file that imports "calculator" which is a local packages. When i run
go test -cover
it only gives the coverage of main but not for the package "calculator" imported by main. i know i can write a test inside calculator and that would do the trick but is there any way possible to get the coverage of locally imported package from main?
Bigger Context - The reason i want to do this is because i have a micro service written in go using gin framework and i want to spin it up as a service and make http calls and further see how the coverage looks like (like component test). I can easily spin it up by writing a main_test go file which starts the service but i am not getting the coverage of the imported packages.
Finally found response here. looks like go has a test binary that can be used
https://www.elastic.co/blog/code-coverage-for-your-golang-system-tests

Why can't I import ClockKit and use CLKComplicationDataSource in an Xcode Playground?

I can import WatchKit and WatchConnectivity and use various delegates like WCSessionDelegate, without any problem.
But when I try to import ClockKit or add CLKComplicationDataSource, Xcode throws errors like "no module exists."
To check this, I created a watch app project and examined ComplicationController.swift. It does have import ClockKit in the source.
Why can't we test this API in an Xcode Playground? Doesn't that defeat the purpose of having a Playground?
No you can't, for the following reasons.
The playground is running on an iOS simulator, not a watchOS simulator. What you want to use or test isn't available in iOS.
The ClockKit framework is more of a collection of objects related to a watch face complication. It wouldn't make sense for it to be available in the playground since the playground itself wouldn't have any complications.
You can't really test a complication in the playground, since no interactive complication server is running in the playground that would call your data source methods.
Complications run in the background, and are managed by the complication server. When your complication is active (enabled) on the watch face, the system wakes your extension in the background, instantiates an instance of your data source, then obtains the necessary data it needs for that complication:
You do not instantiate your data source class explicitly. After defining your class, specify the class name in the General tab of the project settings for your WatchKit extension. When the system needs data, it instantiates your class and initializes it by calling its init method. Once initialized, it calls the corresponding protocol methods to gather any needed data.
You could submit a feature request asking Apple to let you interactively test your complication controller in the playground.

Unable to run tests in Visual Studio

I created a solution with 2 projects, FSharpCalled (which contains a method "disBonjour", transforming a string in another), an F# library, and FSharpTest shown below :
Here's FSharpCalled :
The problem is that I can't run the tests; as shown in the first screenshot, I get an error message saying the "principal module is empty, so nothing will happen at execution".
I tried to modify some properties but without success.
EDIT:
here's the result... damn!
Classes
When you use a let binding inside of a class, the let binding is private (i.e. not publicly accessible). Therefore, you can't use it as a test method.
If you want to use NUnit with a class in F#, you'll have to adorn a method with the [<Test>] attribute:
[<TestFixture>]
type TestClass() =
[<Test>]
member this.SomeTest () =
// Test goes here
()
Last time I used NUnit, you also had to add the [<TestFixture>] attribute to the class itself, so I added that as well. Normally, I don't use NUnit, so I don't know if it's still required.
Modules
You can also write your tests in modules, in which case you can adorn a let bound function with the [<Test>] attribute, since in that case it compiles to a public, static member:
module MyTests =
[<Test>]
let ``a function in a module that works as a test`` () =
// Test goes here
()
At least, the test runner that I use (TestDriven.net) discovers and runs that test as well. I don't know if all other test runners will also do that.
For a general introduction to unit testing with F#, plus much more, you may consider watching my Pluralsight course called Unit Testing with F#.
principal module is empty, so nothing will happen at execution
occurs when you have a console project without an [<EntryPoint>]. So you'd need to either declare such a main method or change the project to be a library project.
Update
Do you use any 3rd party test runner (TestDriven, ReSharper with nUnit integration, ...) or the nUnit VS Test Adapter? VS won't recognize nUnit tests out of the box.

Problems in creating a webtest to test logins

I'm needing some help with a coded web test.
I created a coded web test to see how many accounts are valid to log into my application. I have a lot of accounts (2000+) and I need to know which of them are valid. Basically, I recorded a web test that hits an URL and searches for some certain text that appears in the page after the login. Then I created an xml file containing all account names and passwords and set it as credentials data source. Then modified the testrun.testrunconfig to specify "one test per datasource row" to have the test run for every row in the xml file.
After this, I converted the test to a "coded" web test. So far so good.
The problem arises when I try to create a file (to programmatically add the successful logins in a file). I have a StreamWriter declared privately and try to initialize it in the test constructor, but this throws an error: "could not run webtest xxx on agent yyy: exception has been thrown by the target of an invocation".
I tried to initialize the stream in the same line where it's declared, but I get the same results.
Does anyone have any idea on how can I accomplish the desired test?
I know that I can accomplish this without a coded web test, but to collect the successful login information I have to go line by line in the test result and see what are the ones that passed.
If anyone has a better idea, it's very welcomed!
Best regards
Beto
You can certainly achieve what you are asking, since I have also implemented a similar test.
There must be an error in your code that is causing that exception at runtime.
Instead of using controller/agent rig, try running the test locally first, so that you might get a better error message than the generic "could not run webtest".
Alternatively, if you posted the code perhaps someone could spot the error.
I would follow agentnega's suggestion to run the test locally in order to get a more clear message for the error. Maybe there is something wrong with the file path.
Besides this, I would keep the test as recorded, instead of converting it to a coded one.
I would set a context variable to the path of the file that will have the successful logins at the end, preferably relative to the test deployment directory.
Then write a request plugin class, derived from WebTestRequestPlugin, and override the PostRequest() method in way similar to this one:
public override void PostRequest(object sender, PostRequestEventArgs e)
{
if(Outcome.Pass == e.Request.Outcome)
{
string path = Path.Combine(e.WebTest.Context["$TestDeploymentDir"].ToString(), e.WebTest.Context["logins.txt"].ToString());
StreamWriter sw = null;
if (!File.Exists(path))
{
sw = File.CreateText(path);
}
else
{
sw = File.AppendText(path);
}
sw.WriteLine(e.WebTest.Context["Username"]);
}
}

Resources