如何通过Android Studio运行单元测试?

时间:2016-04-25 23:43:27

标签: android unit-testing android-studio android-gradle build.gradle

我正在尝试通过本机支持在Android Studio中启用单元测试。

Android Studio 1.1+和Android Gradle插件版本1.1.0+增加了单元测试支持。

我目前正在开发的项目使用了 JCandKSolutions android单元测试插件,因为它在本机单元测试支持可用之前就开始了。

我已将当前项目更新为Android Studio 2.1 RC和Android Gradle Plugin Version 2.1.0-rc1。

我已经包含以下测试依赖项:

var scene; var camera; var aspect = window.innerWidth / window.innerHeight; scene = new THREE.Scene(); camera = new THREE.PerspectiveCamera( 45, aspect, 0.1, 1000 ); camera.position.z = 200; var geometry = new THREE.BoxGeometry( 10, 10, 10 ); var loader = new THREE.TextureLoader(); function init() { var posx, posy, posz; var velx, vely, velz; var rotx = 0; var roty = 0; var rotz = 0; var boid; loader.load('images/picture.jpg', function ( texture ) { var material = new THREE.MeshBasicMaterial( { map: texture } ); for ( var i = 0; i < 5; i ++ ) { posx = (Math.random() - 0.5) * 100; posy = (Math.random() - 0.5) * 100; posz = (Math.random() - 0.5) * 100; velx = (Math.random() - 0.7) * 100; vely = (Math.random() - 0.7) * 100; velz = (Math.random() - 0.7) * 100; var boidPosition = new THREE.Vector3(posx, posy, posz); var boidRotation = new THREE.Vector3(rotx, roty, rotz); var boidVelocity = new THREE.Vector3(velx, vely, velz); var boidDesign = new THREE.Mesh( geometry, material ); boidDesign.position.x = boidPosition.x; boidDesign.position.y = boidPosition.y; boidDesign.position.z = boidPosition.z; boid = new Boid(boidPosition, boidVelocity); boids[i] = boid; boidDesign.name = "test_name" + i; scene.add( boidDesign ); } }, function ( xhr ) { console.log( (xhr.loaded / xhr.total * 100) + '% loaded' ); }, function ( xhr ) { console.log( 'An error happened' ); } ); } function redraw() { var boid; loader.load('images/picture.jpg', function ( texture ) { var material = new THREE.MeshBasicMaterial( { map: texture } ); for ( var i = 0; i < boids.length; i ++ ) { boid = boids[i]; var boidPosition = new THREE.Vector3(boid.position.x, boid.position.y, boid.position.z); var boidDesign = new THREE.Mesh( geometry, material ); boidDesign.position.x = boidPosition.x; boidDesign.position.y = boidPosition.y; boidDesign.position.z = boidPosition.z; boidDesign.name = "test_name" + i; scene.add( boidDesign ); } }, function ( xhr ) { console.log( (xhr.loaded / xhr.total * 100) + '% loaded' ); }, function ( xhr ) { console.log( 'An error happened' ); } ); } function removeEntity(object) { var selectedObject = scene.getObjectByName(object.name); scene.remove( selectedObject ); } function drawAfterUpdate() { var boid; for(var i = 0; i < boids.length; i++) { boid = boids[i]; boid.update(boids); removeEntity(boid); } redraw(boids); } var renderer = new THREE.WebGLRenderer(); renderer.setSize( window.innerWidth, window.innerHeight ); renderer.setClearColor( 0x000000 ); document.body.appendChild( renderer.domElement ); var render = function() { requestAnimationFrame( render ); drawAfterUpdate(); renderer.render( scene, camera ); }; init(); render(); testCompile 'junit:junit:4.12'

但是,我仍然无法从Android Studio运行单元测试。

我将通过以下逐步教程添加单元测试:https://io2015codelabs.appspot.com/codelabs/android-studio-testing#1

这是从上面发布的教程中拍摄的照片: [测试工件:单元测试] [1]

我的Android Studio中的Build Variants框没有&#34; Test Artifacts&#34;部分。它只显示Module和Build Variant列,Build Variant只有build.gradle中提到的风格

我错过了什么吗? 任何帮助深表感谢。谢谢!

1 个答案:

答案 0 :(得分:0)

Android Studio不再需要Build Variant选项卡中的Test Artifacts。按照本文所述编写测试后,可以右键单击单元测试文件名(或类名)并选择运行'ExampleUnitTest'。除了ExampleUnitTest之外,它应该是您的单元测试类名称。