简体   繁体   English

测试还是不测试-键盘挂钩功能

[英]To test or not to test - keyboard hook functionality

I've never used TDD and unit testing "properly", but would like to learn some techniques. 我从来没有“适当地”使用过TDD和单元测试,但想学习一些技巧。 Could you please help me with the idea on writing test methods for this not-so-testable (in my opinion) case. 您能否帮助我提出有关为这种不太可测试(我认为)的案例编写测试方法的想法。

The class I want to test is yet to be written (I remember, I need to write the test first), but it will have a method to hook on Windows shortcut key combination (passed as parameter) and will raise an event when that key combination is pressed. 我要测试的类尚未编写(我记得,我需要先编写测试),但是它将有一个方法可以挂接Windows快捷键组合(作为参数传递),并且当该键时会引发事件按下组合键。 So how do I go about testing such a case? 那么我该如何测试这种情况? Do I need to write key press simulation routine first? 我需要首先编写按键模拟程序吗? Do I need a unit test for key press simulation or will the shortcut hook test "cancel out" the need for it? 我需要进行按键模拟的单元测试,还是快捷钩子测试“取消”对它的需求? Is this a unit test or is it called some other way altogether? 这是单元测试还是完全称为其他方式?

The point of this question is rather educational, I perfectly understand that this is not the best case for unit testing and is probably not worth the effort. 这个问题的重点是教育性的,我完全理解这不是单元测试的最佳案例,可能不值得付出努力。 I just want to use non-trivial scenario for better understanding of the principles. 我只想使用非平凡的场景来更好地理解原理。

I would assume you would first start out by writing a test that directly called the Hook method and check that a) some event got raised or b) some value got set in the class under test. 我假设您首先要编写一个直接调用Hook方法的测试,然后检查a)引发了某个事件或b)在被测类中设置了某些值。

The hard stuff like a keyboard event simulator would come later (if it's even needed). 诸如键盘事件模拟器之类的硬东西会在以后出现(如果需要的话)。 This would be the first step and would influence the way you design your hook class. 这将是第一步,并将影响您设计钩子类的方式。 It would hopefully influence it in such a way that makes your hook class extensible enough to work with Windows short cuts and with your unit tests. 希望它将以某种方式影响它,以使您的钩子类具有足够的可扩展性,以使用Windows快捷方式和单元测试。

I tend to agree with cmw. 我倾向于同意cmw。

The general approach is to unit test the logic of the code you have written, not the plumbing you depend on from Windows (or any other outside "service") 通用方法是对所编写代码的逻辑进行单元测试,而不是对Windows(或任何其他外部“服务”)所依赖的管道进行单元测试。

Testing the actual keyboard press functionality, IMO, would be an "integration" test. 测试实际的键盘按键功能IMO将是“集成”测试。 Something done as part of a test script, for instance, that would be performed by QA. 例如,作为测试脚本一部分执行的操作将由质量检查人员执行。

The key to unit testing this scenario is decoupling the dependency on the key press action from the logic in them method that responds to the key press. 对这种情况进行单元测试的关键是将对按键操作的依赖性与响应按键的逻辑方法分离。 This is where unit tests become very valuable. 这是单元测试非常有价值的地方。 They highlight areas of your code that can be re-factored to adhere to the SRP . 它们突出显示了可以重构以符合SRP的代码区域。

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

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