单元测试在游戏编程中可行吗? [英] Is unit testing viable in game programming?

查看:27
本文介绍了单元测试在游戏编程中可行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢单元测试的想法,但我在将其应用于游戏编程时遇到了麻烦.游戏是高度有状态的,并且代码通常不会将自身分解为不同的单元.根据我的经验,大多数函数会改变状态而不是返回值.

考虑一个简单的动作,比如 playerJump(height).我很想有一个测试套件来检查各种各样的情况,以确保跳跃总是按预期工作.然而,这个函数可能不会返回任何值,并且会产生 player.velocity.y = -heightcheckCollisions(player) 的副作用.我想不出一个明确的单元测试来围绕这个进行构建.

单元测试在游戏等高状态应用程序中不可行吗?单元测试的优势是否如此之大以至于值得对游戏进行功能性编程?

<小时>

更新:

Games from Within 有一系列关于在游戏中使用测试驱动开发的深入文章.我强烈推荐给对这个主题感兴趣的任何人.这是第一篇文章:

http://gamesfromwithin.com/stepping-through-the-looking-glass-test-driven-game-development-part-1

解决方案

通常代码不会将自身分解成不同的单元.

那只是糟糕的设计.代码不会将自己分解成任何东西.作为设计者,您必须在代码上强加一个结构,以便您可以证明它确实有效.

然而,这个函数可能不会返回任何值......

所以?

...并且有副作用,player.velocity.y = -heightcheckCollisions(player).

然后测试一下.

我想不出一个清晰的单元测试来围绕这个进行构建.

为什么不呢?你刚刚为函数的结果给出了一个很好的说明.

您可能需要一些模拟对象,以使用更易于测试的简化 MockPlayer 替换成熟的 Player.

但是你的行为规范是完美的.只需测试您描述的内容.

I'm fond of the idea of unit testing but I'm having trouble applying it to game programming. Games are highly stateful and often the code doesn't break itself into distinct units. In my experience, most functions mutate state rather than returning values.

Consider a simple action like playerJump(height). I'd love to have a test suite that checks a large variety of cases to make sure that jumping always works as expected. However this function will likely return no value and have the side effect of, player.velocity.y = -height and checkCollisions(player). I can't think of a clear unit test to build around this.

Is unit testing just not viable in highly stateful applications like games? Are the advantages of unit testing so great that it would be worth programming games functionally?


Update:

Games from Within has a series of in depth articles about using Test Driven Development in games. I highly recommend them to anybody interested in this subject. Here is the first article:

http://gamesfromwithin.com/stepping-through-the-looking-glass-test-driven-game-development-part-1

解决方案

Often the code doesn't break itself into distinct units.

That's just poor design. The code doesn't break itself into anything. You, the designer, have to impose a structure on the code so that you can demonstrate that it actually works.

However, this function will likely return no value...

So?

...and have the side effect of, player.velocity.y = -height and checkCollisions(player).

Then test for that.

I can't think of a clear unit test to build around this.

Why not? You just gave an excellent specification for the results of the function.

You might need a few mock objects to replace the full-blown Player with a simplified MockPlayer that's easier to test with.

But your specification of behavior was perfect. Just test for the things you described.

这篇关于单元测试在游戏编程中可行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆