简体   繁体   English

如何对 API 进行单元测试?

[英]how can i do unit testing for APIs?

我是单元测试的新手,我的任务是为 Post API 编写单元测试,我只有终点和 api 主体,我在谷歌阅读了很多,但我不知道如何开始,有什么帮助吗?

You must create a intent, fill it with data if needed, like the data extracted from the QR code and then call startActivity.你必须创建一个意图,如果需要,用数据填充它,比如从二维码中提取的数据,然后调用 startActivity。

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle b = new Bundle();
b.putInt("key", 1); //Your id
intent.putExtras(b); //Put your id to your next Intent
startActivity(intent);
finish();

I put an int in the bundle but it could be anything that implements Serializable.我在包中放了一个 int 但它可以是任何实现 Serializable 的东西。

You can reach direct on required screen with that pages's activity (as shown Splash screen in below example).您可以使用该页面的活动直接到达所需的屏幕(如下例中的Splash screen所示)。 You can launch activity with below method.您可以使用以下方法启动活动。

public static void launchActivity(Activity activityName) 
{

        ((AndroidDriver<MobileElement>) driver).startActivity(activityName);
}

How you can call this function如何调用此函数

Suppose you have below app package and activity (its for example but you have to use for your app)假设您有以下应用程序包和活动(例如,但您必须将其用于您的应用程序)

String appPackage ="my.app.helloworld";

String appActivity = "my.app.helloworld".common.activity.SplashScreen";

launchActivity(new Activity(appPackage, appActivity));

You need to set android:exported="true" in your AndroidManifest.xml file to resolve error java.lang.SecurityException您需要在AndroidManifest.xml文件中设置android:exported="true"以解决错误java.lang.SecurityException

<activity
    android:name="com.dsquares.lucky/.screens.mainscreens.Wallet.WalletPayment.AddFundsActivity"
    android:label="wallet" 
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" >
        </action>
    </intent-filter>
</activity>

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

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