Test and profile your Windows Phone 8 App
-
Upload
michele-capra -
Category
Technology
-
view
484 -
download
1
description
Transcript of Test and profile your Windows Phone 8 App
WPReborn 2013
Michele CapraSoftware Engineer @ OrangeCode
___________________________________________________
Test and profile your application
@dnlombardia#wpreborn
What we’re going to see:
• Testing in WP7
• Testing tools for WP8
• Profiling tools
Who am I• Freelance Developer
• C#, Asp.net mvc, WPF , Windows Phone 7 – 8
• Python, Django
• Blog: orangecode.it/blog
Where we were
Unit / Integration test : - Microsoft Test - Nunit for Windows Phone 7
Mocking framework:- Moq 3.1 for Silverlight
Test runner for Windows Phone 7
• Nunit runner
• Resharper
• Test Driven.net
Solution structure in Windows Phone 7WP7 App
WP7 Class Library
TestSuite
View and ViewModel
Testing in Windows Phone 8
- Windows Phone 8 new feature
- Asyncronous programming style
- Need of an emulator to run the code- No test runner can be used directly in Visual Studio
Windows Phone Toolkit Test Framework
• Part of the Windows Phone Toolkit
• Provides an easy way to integrate unit tests for your Windows Phone apps
Windows Phone Toolkit Test Framework
• Test suites are no longer Class Libraries but Applications
• No more automated run
• Can manage asycnronous code
How to setup WP8 testing tools• Reference via nuget: Windows Phone Toolkit
Test Framework• On your MainPage costructor: public partial class MainPage : PhoneApplicationPage { public MainPage() { InitializeComponent(); Content = UnitTestSystem.CreateTestPage(); } }
How to setup WP8 testing tools
Test example scenario
• App loads products informations from db and shows it
• SQLite as db engine
• ModelView-View-Model pattern
Solution structure
First test [TestClass] public class MainViewModelFixture { private MainViewModel _viewModel; private Mock<IDbService> _service; public MainViewModelFixture() { _service= new Mock<IDbService>(); _viewModel= new MainViewModel(_service.Object); } [TestMethod] public void Initialize_Should_LoadDataFromDb() { _viewModel.Initialize(); _service.Verify(p=>p.LoadProducts()); } }
Run test
Red to Greenusing Caliburn.Micro;using OrangeCode.Wpreborn.SQLIteApp.Services;namespace OrangeCode.Wpreborn.SQLIteApp.ViewModels{ public class MainViewModel :PropertyChangedBase { private readonly IDbService _service;
public MainViewModel(IDbService service) { _service = service; } public void Initialize() { _service.LoadProducts(); } }}
Red to Green
Second test [TestMethod] public void Initialize_Should_ShowData() { _service.Setup(p => p.LoadProducts()).Returns(
new List<Product>{new Product {Name = "Product 1", Serial =
"123456"} } );
_viewModel.Initialize();
Assert.AreEqual(_viewModel.Products.Count,1); Assert.AreEqual(_viewModel.Products[0].Serial,"123456"); }
Second test public class MainViewModel :PropertyChangedBase { private readonly IDbService _service; public IList<Product> Products { get; set; }
public MainViewModel(IDbService service) { _service = service; } public void Initialize() { Products=_service.LoadProducts(); } }
Second test
Asynchronous testingHow to test this?
public interface IDbServiceAsync{ Task<IList<Product>> LoadProductsAsync(); } public class DbServiceAsync :IDbServiceAsync{ private readonly SQLiteAsyncConnection _context; public DbService(SQLiteAsyncConnection context) { _context = context; } public async Task<IList<Product>> LoadProductsAsync() { return await _context.Table<Product>().ToListAsync(); } }
Asynchronous testingThat’s the easy way to test it..
private async void PrepareDb() { SQLiteAsyncConnection connection = new SQLiteAsyncConnection("TestDb.sqlite”); await connection.DropTableAsync<Product>(); await connection.CreateTableAsync<Product>(); await connection.InsertAsync(new Product { Name = "Product 1", Serial = "123456" }); } [TestMethod] public async void LoadProductsAsync_Should_Load_Data_FromDb() { await PrepareDb(); var data= await _service.LoadProductsAsync(); Assert.AreEqual(data.Count,1); }
Let’s run the test suite
FeelingsWhat I like: - Test real code- Async / await runner support
What I dislike: - No automated runner- No class library support
Profiling appVisual Studio 2012 provides the Windows Phone Application Analysis tool.
Main feature:• App Monitoring: you can evaluate the most important
behaviors of your app that contribute to a good user experience, such as start time and responsiveness.
• Profiling: you can evaluate either execution-related or memory-usage aspects of your app.
Phone application anaysis tool
Test app
Running dei profiling
Phone application report
Report Details
Memory good practice
- Implement IDisposable in ViewModel
- Unload all event listener
- Consider using VirtualizingStackPanel instead of StackPanel
Quick recap
- Windows Phone Toolkit Test Framework
- Windows Phone Application Analysis
That’s all folks- GitHub repos:
- Testing : https://github.com/piccoloaiutante/WP8TestingExample
- Profiling: https://github.com/piccoloaiutante/WP8ProfilingExample
- Contact:
Email : [email protected] Blog: www.orangecode.it/blog
Twitter: @piccoloaiutante GitHub: github.com/piccoloaiutante