Moq newbie test setup - unit-testing

I'm coming up to speed with the Moq framework and I have a button on a form that launches another form.
The client wants the form to launch whether the Click or DoubleClick event is fired off.
My understanding of the Moq framework is that it is useful when you want to simulate certain types of actions: (For brevity, I'm only listing a few types of actions)
Connect to a database
Write to a file
Determine network connectivity
But I'm not 100% certain on how Moq can interact with Windows controls and events.
Can anyone put me in the right direction with a simple example?

I wouldn't use Moq or unit testing for that scenario.
You have code under a visual control that launches another form. That's inherently not testable in isolation. You could automate integration tests with other tools if you wanted to.
Moq is designed for enabling unit testing. Unit testing tests functionality (often logic) in isolation. It's difficult to apply to user interfaces.
Normally what you'd do is go with one of the Model-View-Controller variants, separate your presentation layer from your business logic, use interfaces and Dependency Injection to break dependencies, then use an isolation framework such as Moq to create fake implementations of those interfaces so that you could test subsystems independently.
If this sounds complicated, it is. It's also critical to writing large, tested applications.
I'd recommend starting by reading the book The Art of Unit Testing with Examples in .NET by Roy Osherove. That covers a lot of the basics.

Related

Grails unit/intergration testing

I'm trying to add unit-test and integration-test for my grails application but I have some trouble how to distinguish between both and when to use unit or integration to test my controllers actions and services.
The tutorial I found online is not very clean. I can't find complete example to follow up.
Can you please share helpful topics?
I follow the following guidelines:
Try writing as many unit tests as you can. They can be written for controllers, services, domain classes or any other groovy classes. The idea is that unit tests are friends for developers. Writing enough unit tests will
make sure that the developer makes lesser mistakes. As they execute
quickly, this means quick verification. But unit tests cannot test the following:
Criteria queries, HQL queries
Actual database Interactions (queries, transactional behaviour, updates, db constraints etc.)
Inter modular interactions
So we write the Integration tests as well
Integration tests take longer to execute. Writing Integration tests often need bootstrapping data. But they really are helpful to test functionalities end to end (excluding the actual user interactions through UI for which functional tests are written). So Integration Tests can be written for:
Testing all database interactions as unit tests actually do not test the database interactions. This also includes testing criteria, hql etc.,
Testing transactional behaviour (which is dependent on db)
Testing implementations end to end. So this will also test how two independently created modules interact with each other and make sure we have created them correctly.
One problem with integration tests is their speed. For me, integration tests take 15+ seconds to start up. In that time, certain things do slip out of mind focus.
I prefer to go with unit tests that start in no more then 2 sec and can be run several times in those 15 seconds.
One more argument for unit tests is they force you to decouple your code. Integration tests always tempt you to just rely on some other component existing and initialized.
Important links:
http://spockframework.org/spock/docs/1.0/interaction_based_testing.html
http://docs.grails.org/latest/guide/testing.html
Unfortunately it is not just a matter preference or speed. It is a huge subject, but I can give you some advice based on my experience.
If you expect to be covering your database access code (queries, transactional behaviour) by using unit tests, you are deluding yourself. You are testing how your queries comply with the in-memory implementation of GORM. Not hibernate, not your database.
I usually have two types of tests. Unit and functional tests. The functional tests will perform a full test, running against a real database, and stimulating the system like a user would (if it is a web site via Geb, if it is a REST api, via a REST client).
The functional tests will set up a startup state by executing some kind of fixture code first. This can be registering a user and logging them in, for example. Then the test will run, and then the postconditions are checked. Here, you can check the postconditions either by accessing the database through the GORM API, or by using production API calls (danger of covering a bug with another bug).
Sometimes, your system will interact with a third system. Here, if you can, you can mock the implementation of the third system, by injecting a mock implementation into the system under test.
You have also tools like Spring Cloud Contract, that allow you to create bock a mock server for your system under test, and a specification for your third-party system. See https://cloud.spring.io/spring-cloud-contract/spring-cloud-contract.html
The unit tests, I use to thoroughly test all execution paths of a given class. I will try to trigger all exception states, all secondary scenarios, to make sure that everything is covered. I don't think it is realistic to have 100% coverage by using functional or integration tests.

Test Automation (Flow)

My Intention is to test the functional flows of the application. So i think best way is testing UI for the steps of the functional flow. (E.g : Add item, after search it and add pricing etc.). My concern is to find best automation tool which should support for desktop application. does anyone have handheld experience of doing this. As a coding guy i don't have experience in Testing.
Additionally: the application is huge application which have connected with oracle database. So it need to clear the database as startup of the testing. So tool should support this matter also.
Programming language : C#
I found Selenium (but this is only for web application), Renorex and IBM rational Functional Tester.
is my approach(test UI steps for Functional steps) correct? have any one work on above tools or any other best tools whcih support to C# windows application.
I think NUnit is best for text unit test in this project. am i correct? according to my research google i found this as result.
I'll try to keep it short. A good fit for you case
automation tool which should support for desktop application.
and
Programming language : C#
will be the VisualStudio's build in Coded UI test. All actions performed on the desktop will be recorded. Pause the recording if you are performing actions that may lead to sensitive data being included in the recording. When coded UI tests are automated, they have to interact with the desktop when you run them, unlike other automated tests.
As far as you try to keep the tests close to the Test pyramid
the QA effort should be acceptable and your
approach(test UI steps for Functional steps)
will be correct and feasible. In general try to avoid long and fragile tests, except for some E2E tests that will exercise the (most business valuable) end-user journeys.
With C# should be relatively easy to drop oracle database for your
need to clear the database as startup of the testing.
Such DB cleaning is often used Fixture strategy. Take a look at this Persistent Fixture. Usage of such DB fixtures can be put to work for all conditions that your UI tests expect/rely on (avoid UI setup for tests), example - create available and applicable bonuses.
Another way for your UI layer tests could be some of the .Net based Sikuli implementations:
SikuliSharp
SikuliIntegrator
sikuli4net

Can I use Protractor for TDD in MEAN stack

I'm very new to Unit testing and TDD. I'm clear with TDD concepts theoretically, but I'm having lots of impediments in implementing that. Most of the examples explains how to do unit testing for Multiply, adding two numbers, etc., which is not we really need in real time.
For angular, it is much better, we can check the values of array, existence of Controller, using the service, mocking the backend, etc., So now I have couple of questions,
How can do Unit testing for Backend process like how the request is being handled ?
My application mostly interacts with UI components, Can I use protractor in my TDD process, for example Drawing tool, how can I do testing without drawing(interacting) anything on it ?
There are many orm framework available in nodejs to generate testdata if you want to generate data in your traditional DB and use your backend as it is.
jugglingdb and sequelizejs are the most popular one.
Even to make it more manageable you can implement cucumber or jasmine framework with protractor, so you can manage after and before hookups for individual test-scenario.

How to get started with TDD using Winforms

I have read/watched so much about TDD & BDD recently that I really want to master it. I have been a developer that only writes code and then tests it from outside (like we always started). The problem seems to be in getting up and running with TDD. I want to just create a simple Winform app in which I want to show a list of something lets say products. I just don't know where to get started, should I write a test for controller first? the controller needs reference to view and service and so on so forth. ASP.Net MVC is built for testing therefore it is a bit easy to get started but Winforms are a real pain. Kindly give me some Videos (Most preffered) that show TDD in Winforms.
I have watched tons of videos that show you testing a class or feature but how do you test UI that does not support testing?
In short I want to know if anyone has been doing TDD for a while, how
does he/she do it in Winforms?
I have written loads of code that I just delete because I get stuck, Please help!
Here's how I do it for any kind of UI, be it Web or Winforms or WPF or Swing in Java or even a web service interface that's going to be used by another system.
Write the UI, and hard-code any data behind it.
Write a controller, and move the hard-coded data to the controller. The controller should just be presenting the data in a form that the UI can understand.
Your controller now has no behavior; just static data - but it's got the right API for the UI, and now you know how the UI wants to use it.
Write an example that shows how the UI will use the controller in the most basic form.
Make the example work.
Write another example that shows how the controller behaves differently in a different context.
Make the example work.
Those are your unit tests! If you know that your controller is going to need some other classes to collaborate with, you can mock those out too.
Once your collaborators are mocked out, you already know how the controller wants to use them. Again, you have the API for those collaborators already, because the controller is using them.
This is what we're doing when we talk about "outside-in" in BDD.
I'm new to TDD too, and just like you I'm trying to learn. Here's what I've come up with during my searches, maybe it will help you too:
Check out Roy Osherove's website, and also his book "The Art of Unit Testing". Check out the videos, there are some great ones, especially the "Pair Programming" section. His book was the single best book that I've read about unit testing and TDD, and the only one that I've read from cover to cover.
Check out some TDD katas. There are some on the web page I've suggested in the previous item. Check out how other people are solving the katas. It's really helpful.
Read about dependency injection.
If you want to TDD in WinForms, check out the MVP pattern. As far as I know, it's the de facto patten for separating UI from business code in WinForms.
Good luck with your search.
The controller is a good starting point. Extract the View public api to an interface an test the interaction of the controller with that interface using a Mocking Framework. You can do the same for the service layer too.
Also, I would take the bottom-up approach for this application and unit test the lower level layers before adding tests to the UI layer. On the UI layer I will write a set of Acceptance Tests using a BDD framework and use mocking frameworks to make those tests lightweight and to reduce the number of tests.
Good luck!

How to unit test wxPython?

I've heard of unit testing, and written some tests myself just as tests but never used any testing frameworks. Now I'm writing a wxPython GUI for some in-house data analysis/visualisation libraries. I've read some of the obvious Google results, like http://wiki.wxpython.org/Unit%20Testing%20with%20wxPython and its link http://pywinauto.openqa.org/ but am still uncertain where to start.
Does anyone have experience or good references for someone who sort of knows the theory but has never used any of the frameworks and has no idea how it works with GUIs?
I am on a Windows machine developing a theoretically cross-platform application that uses NumPy, Matplotlib, Newville's MPlot package, and wxPython 2.8.11. Python 2.6 with plans for 3.1. I work for a bunch of scientists, so there is no in-house unit-testing policy.
If you want to unit-test your application, you haven't to focus on GUI testing techniques. It is much better to write the application using MVC, MVP, or other meta-pattern like these. So you get business logic and presentation layer separated.
It is much more important to cover the business layer with tests since this is your code. Presentation layer is tested already by wxWidgets developers. To test the business layer it will be enough just basic tools like standard unittest module and maybe nose.
To make sure the whole application behave correctly, you should add few acceptance tests that will test functionality from end to end. These will deal with GUI, but there will be few such tests comparing to number of unit-tests.
If you will limit yourself with acceptance tests only, you'll get low coverage, fragile and very slow test code base.
To unit test your application without requiring lots of mock objects/stubs, your GUI's event handlers should basically delegate to other method calls, passing in values from the Event object as parameters to the delegated method.
Otherwise you'll be unable to test your application without having to mock wx's objects.
Take a look at the PyPubSub project for a great module to help with MVC.
In one early project of mine I really test wxPython application using GUI layer. So tests really spin live wxApp object, pops up real windows and then starts messing with a real MainLoop(). Very soon I realize it was a wrong way to do testing. My tests was run very slow and unreliable. Much better way is to separate GUI stuff aside and test only the "model" level of your application. Note that you can actually create model for presentation level logic (model that represent some visual part of your application) and test it. But this model should not involve any "real" gui objects (windows, dialogs, widgets).