初了解Junit3.8.2&Junit4

本文分享了一位程序员从接触单元测试到初步掌握JUnit4的心路历程。文章介绍了JUnit4的基本概念,如测试类、测试方法、fixture的设置与清理,以及测试套件等,并强调了单元测试的重要性。

       在这儿安家已经好久了,就是没能上来写东西,实习这段时间,项目组的一个大牛极力推荐写代码多写测试类,刚开始的时候也不知道写测试类干嘛,再说自己刚开始学习写代码根本没那个能力,乱七八糟的代码写出来只要能实行应有的功能就已经感觉已经很庆幸了,代码写的像大牛说的那样天马行空了。。。。。跑题了,那时候改点东西运行一下看看是否达到预期的结果慢的要死,那会儿几乎一天只能写一点点的东西,那会儿大牛说的用junit写测试类压根就没考虑,慢慢,慢慢的感觉到自己写代码的效率是非常的低,最近由于时间比较多,大牛有时候给我们讲点新东西,有提到了junit,听大牛说的,想想自己学习的这段过程中写代码效率低,是不是应该学习学习junit,今天找了些junit的东西看了看,找到的是关于4的相关资料,junit用java5的annotation使得写测试类更加简单(在我没看junit3.x前我也不知道倒底简单了多少,看了3.8的一点资料后发现果然简单了许多),由于一直用的java1.4.2,对annotation的特性还没有多少了解,硬着头皮把那篇文章看完了,在junit4中涉及倒的概念有测试类(方法),这是最基础的,在3.8中很注重测试方法的命名必须以Test开头的类,而在4中你可以用annotation特性,在测试方法前加@Test就不必特别小心测试类的命名,测试方法必须是public void的,记住:您的单元测试代码不是用来证明您是对的,而是为了证明您没有错。因此单元测试的范围要全面,比如对边界值、正常值、错误值得测试;对代码可能出现的问题要全面预测,而这也正是需求分析、详细设计环节中要考虑的,(这是引的那篇文章的原话)要牢记!请牢记这一条 JUnit 最佳实践:测试任何可能的错误。单元测试不是用来证明您是对的,而是为了证明您没有错。测试有failure和error两种结果,failure表示测试点出现问题,说明我们写的类有问题,error是由代码异常引起,可能是测试类代码问题,也有可能是要测试的代码潜在的bug。  下一个概念就是fixture,就是一个测试方法或多个测试方法的的一系列公共数据,在4中junit的提供了方法定义fixture,应用annotation,junit4应用注解before定义初始fixtrue方法和注解after定义fixture用完后注销方法,before,after只能一次运行一个测试方法,也就是说初始一次fixture运行测试方法注销fixture,运行下一个又要初始fixtrue,又要注销。。。。。还是有点麻烦,要使所有的测试方法运行只需一次初始和一次注销,那么就要用注解beforeClass和afterClass,在3.8中用setUp()初始fixture和tearDown()注销fixture,在3.8中是不是也有类似的一次初始化fixture和注销fixture我还不清楚(嘻嘻,毕竟只是开始看,慢慢来),希望高手来指点。还有一个junit中一个重要的概念就是测试套件(Suite),就是junit提供的批量运行测试类的方法,主要原因写的代码的类越多,方法越多一次只运行一个测试方法,这样测试起来效率就慢了,批运行就快许多。。。。。今天就到这里吧,感觉了解的东西还挺多的,我理解的肯定有许多不正确的地方,希望高手多指点,谢谢,我会继续学习junit的。

### USACO 2016 January Contest Subsequences Summing to Sevens Problem Solution and Explanation In this problem from the USACO contest, one is tasked with finding the size of the largest contiguous subsequence where the sum of elements (IDs) within that subsequence is divisible by seven. The input consists of an array representing cow IDs, and the goal is to determine how many cows are part of the longest sequence meeting these criteria; if no valid sequences exist, zero should be returned. To solve this challenge efficiently without checking all possible subsequences explicitly—which would lead to poor performance—a more sophisticated approach using prefix sums modulo 7 can be applied[^1]. By maintaining a record of seen remainders when dividing cumulative totals up until each point in the list by 7 along with their earliest occurrence index, it becomes feasible to identify qualifying segments quickly whenever another instance of any remainder reappears later on during iteration through the dataset[^2]. For implementation purposes: - Initialize variables `max_length` set initially at 0 for tracking maximum length found so far. - Use dictionary or similar structure named `remainder_positions`, starting off only knowing position `-1` maps to remainder `0`. - Iterate over given numbers while updating current_sum % 7 as you go. - Check whether updated value already exists inside your tracker (`remainder_positions`). If yes, compare distance between now versus stored location against max_length variable's content—update accordingly if greater than previous best result noted down previously. - Finally add entry into mapping table linking latest encountered modulus outcome back towards its corresponding spot within enumeration process just completed successfully after loop ends normally. Below shows Python code implementing described logic effectively handling edge cases gracefully too: ```python def find_largest_subsequence_divisible_by_seven(cow_ids): max_length = 0 remainder_positions = {0: -1} current_sum = 0 for i, id_value in enumerate(cow_ids): current_sum += id_value mod_result = current_sum % 7 if mod_result not in remainder_positions: remainder_positions[mod_result] = i else: start_index = remainder_positions[mod_result] segment_size = i - start_index if segment_size > max_length: max_length = segment_size return max_length ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值