本文介绍了最简单的方法来进行单元测试的机器人库应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,如果这是一个有点模糊的问题,但即时通讯努力寻找一个的的例子就怎么做单元测试(隔离测试)与Android ...

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...

下面是什么,我想实现的一个例子:

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);
    }
}

所以,一个项目包含模型和逻辑,那么另外一个项目包含测试说库。没有前端或UI,所以我想做的最低限度只能够测试我的方法,所有的工作孤立的。

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.

推荐答案

只要你的库中不包含在Android SDK中对资源的任何引用,没有任何更多的这个比普通的单元测试。在Eclipse工作区,说你有你的主要项目 MyAndroidLibProject ,您只需创建一个新的Java项目(如 MyAndroidLibProjectUnitTests ) 。里面在这里,您可以创建普通的单元测试指的是在主项目中的计算器类(只是确保你的主要项目添加到您的测试项目的构建路径)。

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. In your Eclipse workspace, say you have your main project MyAndroidLibProject, you simply create a new Java Project (e.g. 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).

您可能还会发现在类似的问题我之前问自己,还有answer到了这个问题。

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);
    }
}

以上是我自己的测试学期类(一个简单的数据类再presenting一学期)。 学期位于我的Andr​​oid项目中 MyMainProject (但是类本身不包含Android SDK中的任何引用) 。 SemesterTest 位于我的测试项目中为MyTestProject (一个普通的Java项目),既 MyMainProject 为MyTestProject 在同一个工作区是(而且由于 SemesterTest 具有相同的包名称学期,我不需要任何特殊的导入语句引用学期其一)。 为MyTestProject 也有 MyMainProject 添加到它的构建路径(的junit.jar 的也加入到构建路径,但这种自动发生,至少在Eclipse中,我认为)。

The above is a test of my own Semester class (a simple data class representing a semester). Semester is located inside my android project MyMainProject (but the class itself doesn't contain any references to the 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). 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).

因此​​,大家可以看到,这不过是一个完全普通的单元测试(JUnit 4中,只是提到它)。希望这有助于。

So as you can see, this is nothing but a completely ordinary unit test (JUnit 4, just to have mentioned it). Hope this helps.

这篇关于最简单的方法来进行单元测试的机器人库应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 07:42