Windows Phone unit test

Source: Internet
Author: User

After WP8, Microsoft launched its own testing framework, called Windows Phone toolkit test framework. This testing framework is supported by both WP7 and WP8.

Today, we will introduce how to install and use this testing framework.

1. We recommend that you use vs 2012 for Windows Phone and install the Windows Phone toolkit and Windows Phone toolkit test framework libraries using the nuget library management tool. (If you are not using the tool, go to Baidu for nuget)

2. Enter the Windows Phone toolkit and Windows Phone toolkit test framework to install the toolkit.

3. after the installation, you will find that nuget will reference three libraries to the project, such. it's easy. if we download it on codeplex, We need to decompress it and then introduce it to the project manually. However, using nuget will be in place in one step.

4. Start the test project. First, add the following code to the mainpage constructor to run the project. Check the test page.

 public MainPage()        {            InitializeComponent();            this.Content = UnitTestSystem.CreateTestPage();        }

5. you will see this page, click the button above the AppBar to start testing, of course. our test project does not contain the logic to be tested, and the running tests will always be displayed ....

6. Of course. If you want to write a class and test the function of the class,

Before using this framework, let's take a look at what unit testing is. I found the following information on the Internet:

What is unit test?

Unit testing is a short piece of code written by developers. It is used to check whether a very small and clear function of the tested code is correct. Generally, a unit test is used to determine the behavior of a specific function under a specific condition (or scenario. For example, you may put a large value into an ordered list, and then confirm that the value appears at the end of the list. Alternatively, you may remove characters that match a certain pattern from the string and confirm that the string does not contain these characters.

Unit testing is a minimal test to test a function or code block. Typically, programmers rather than testers need to know the details of internal program design and coding. This work is not easy to do unless the application system has a well-designed architecture; Development of test drive modules or test kits may also be required

Let's take a look at this article and click to open the link.

7. Create a new class and add the following code. The access permission of this class must be public. Otherwise, after you click test, the class will remain stuck in the runningtests... status. No test result is displayed.

[Testclass] tells the framework that we want to test this class. [testmethod] indicates that this method should be tested. there is also the [classinitialize] label, which runs before running the [testmethod] label method of any application. It is very suitable for creating test data and initializing test conditions.

The [testcleanup] label method of the application runs after the [testmethod] label method of all applications is run. This method is very suitable for cleaning test data.

In theory, the [testmethod] label methods of each application are independent, and they should be able to run in any order. however, some methods may have side effects, such as adding, updating, or deleting data in the database. To avoid interference from these factors, you can apply the [testinitailize] and [testcleanup] labels to create an independent context for each method.

  [TestClass]   public class MyTestCase    {         [TestMethod]          public void SimpleTest()          {              int a = 2;              int b = 2;              int c = a + b;              Assert.IsTrue(c == 4);          }     }

Run it.

This method has passed the test.

8. In actual use, you need to test the code in the company project. You cannot copy the code in the project to the test project.

I created a testdata project in the project, which is equivalent to a project in the company, and then referenced the testdata project in the WP8 unit test,

 

I added the following test code to the test project.

[Testclass] public class mytestcase {[testmethod] public void simpletest () {int A = 2; int B = 2; int c = a + B; assert. istrue (C = 4);} // test my project [testmethod] public void testsearch () {studentslistviewmodel studentsviewmodel = new studentslistviewmodel ("male"); studentsviewmodel. searchstudentbysexcommand. execute (null); assert. istrue (studentsviewmodel. issearchsuccess = true);} private student [] students; [testinitialize] public void initdatabase () {STUDENTS = new student [] {New Student {sex = "male ", name = "Test 1", inclass = "Class 1",}, new student {sex = "female", name = "Test 1 ", inclass = "Class 1" ,}, new student {sex = "male", name = "Test 1", inclass = "Class 1 ",}, new student {sex = "male", name = "Test 1", inclass = "Class 1", }}; testdata. app. localdatabase. students. insertallonsubmit (students); testdata. app. localdatabase. submitchanges ();} [testcleanup] public void cleanupdatabase () {testdata. app. localdatabase. students. deleteallonsubmit (students); testdata. app. localdatabase. submitchanges ();}}

I created an SQL Server ce database in the testdata project to save the student table. I first added several student records to the database in the initdatabase () method of the test project, and then used testsearch () the gender-based search function was tested,

In cleanupdatabase (), the data in the database is deleted.

Test results:

The code is here. Click the open link

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.