简体   繁体   English

对Android库应用程序进行单元测试的最简单方法?

[英]Simplest way to unit test an android library application?

Sorry if this is a bit of a vague question, however im struggling to find a single solid example on how to do unit testing (isolated testing) with Android... 抱歉,这是一个模糊的问题,但是我很难找到一个可靠的示例来说明如何使用Android进行单元测试(隔离测试)...

Here is an example of what i'm wanting to achieve: 这是我要实现的示例:

// Some class
class Calculator
{
    public int Add(int a, int b) { return a+b; }
}


// Simple test
import org.junit.Assert;
import org.junit.Test;

class CalculatorTests
{
    @Test
    public void should_add_numbers_correctly()
    {
        Calculator calculator = new Calculator();
        int expectedResult = 5 + 5;
        int actualResult = calculator.Add(5,5);
        Assert.assertEqual(actualResult, expectedResult);
    }   
}

So one project contains models and logic, then another project contains tests for said library. 因此,一个项目包含模型和逻辑,然后另一个项目包含针对所述库的测试。 There is no front end or UI, so I want to do the bare minimum to just be able to test that my methods all work in isolation. 没有前端或UI,因此我想做最少的工作,以便能够测试我的方法都是独立工作的。

As long as your "library" doesn't contain any references to resources in the Android SDK, there isn't anything more to this than regular unit testing. 只要您的“库”在Android SDK中不包含对资源的任何引用,除了常规的单元测试外,没有任何其他用途。 In your Eclipse workspace, say you have your main project MyAndroidLibProject , you simply create a new Java Project (eg MyAndroidLibProjectUnitTests ). 在Eclipse工作区中,假设您有主项目MyAndroidLibProject ,您只需创建一个新的Java项目(例如MyAndroidLibProjectUnitTests )。 Inside here, you create ordinary unit tests referring to the Calculator class in your main project (just make sure that your main project is added to the build path of your test project). 在此处内部,您将引用主项目中的Calculator类创建普通的单元测试(只需确保将主项目添加到测试项目的构建路径中即可)。

You might also find some additional information in a similar question I asked myself earlier, as well as the answer to that question. 您可能还会在我之前问自己的类似问题中找到一些其他信息,以及该问题的答案

Updated with example: 更新示例:

import static org.junit.Assert.*;
import org.junit.Test;

public class SemesterTest
{
    @Test
    public void aNewSemesterShouldHaveANegativeId()
    {
        Semester semester = new Semester(2010, SemesterType.FALL);
        assertEquals(-1, semester.getInternalId());
    }

    @Test
    public void toStringShouldPrintSemesterTypeAndYear()
    {
        Semester semester = new Semester(2010, SemesterType.FALL);
        assertEquals(SemesterType.FALL + " 2010", semester.toString());
    }

    @Test
    public void equalityShouldOnlyDependOnSemesterTypeAndYear()
    {
        Semester aSemester = new Semester(2010, SemesterType.FALL);
        aSemester.setInternalId(1);

        Semester anotherSemester = new Semester(2010, SemesterType.FALL);
        anotherSemester.setInternalId(2);

        assertEquals(aSemester, anotherSemester);
    }
}

The above is a test of my own Semester class (a simple data class representing a semester). 上面是对我自己的Semester类(代表一个学期的简单数据类)的测试。 Semester is located inside my android project MyMainProject (but the class itself doesn't contain any references to the Android SDK). Semester位于我的android项目MyMainProject (但类本身不包含对Android SDK的任何引用)。 SemesterTest is located inside my test project MyTestProject (an ordinary Java project), with both MyMainProject and MyTestProject being in the same workspace (and since SemesterTest has the same package name as Semester , I don't need any special import statement to reference Semester either). SemesterTest位于我的测试项目中MyTestProject (一个普通的Java项目),既MyMainProjectMyTestProject在同一个工作空间都(和自SemesterTest有一样的包名Semester ,我不需要任何特殊的import语句参考Semester任)。 MyTestProject also has MyMainProject added to its build path ( junit.jar is also added to the build path, but this happens automatically, at least in Eclipse I think). MyTestProject还向其构建路径添加了MyMainProjectjunit.jar也已添加至构建路径,但这至少在我认为的Eclipse中自动发生)。

So as you can see, this is nothing but a completely ordinary unit test (JUnit 4, just to have mentioned it). 正如您所看到的,这不过是一个完全普通的单元测试(JUnit 4,仅此而已)。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM