如何断定日期时间戳是否在2分钟之内?

huangapple 未分类评论69阅读模式
标题翻译

how to assert date time stamp is within 2 minutes?

问题

以下是您要翻译的内容:

我想要添加断言来确保检索到的数据在以下时间范围内:

  1. 打印数据1
    2020-03-16 09:08:49

  2. 打印数据2
    2020-03-16 09:09:15

  3. 断言数据1和数据2之间的时间间隔不超过2分钟,通过。

我有一些示例代码,这是最佳方法吗?
有什么建议,请评论。

  1. // data1
  2. Date data1 = new Date();
  3. // data2
  4. Date data2 = new Date();
  5. // 断言
  6. assertThat(data1, DateMatchers.within(2, ChronoUnit.MINUTES, data2 ));

最新脚本:

  1. import static org.assertj.core.api.Assertions.*
  2. import java.sql.*
  3. import java.text.SimpleDateFormat
  4. import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
  5. import internal.GlobalVariable as GlobalVariable
  6. import java.text.ParseException
  7. import java.text.SimpleDateFormat
  8. import java.util.Date
  9. import com.kms.katalon.core.configuration.RunConfiguration
  10. GlobalVariable.TestIssueKey = null
  11. WebUI.delay(1)
  12. // SQL 语句
  13. dbQuery2 = /SELECT * FROM drugs.sync/
  14. // 连接到 PostgresSQL,全局变量存储在配置文件中
  15. List results = CustomKeywords.'test.database.getPostgresSQLResults'(GlobalVariable.dbConnString2 , GlobalVariable.dbUsername2 , GlobalVariable.dbPassword2 ,GlobalVariable.dbDriver2 ,dbQuery2 )
  16. // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
  17. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss", Locale.ENGLISH)
  18. String date = sdf.format(new Date())
  19. // 打印 "lastupdatedwm6" 列用于 PULL
  20. String lastupdatedwm6 = results.get(0).get('lastupdatedwm6')
  21. // 将 lastupdatedwm6 存储到文件
  22. def lastupdatedwm6aft = new File(RunConfiguration.getProjectDir() + "/Data Files/lastupdatedwm6aft.txt")
  23. lastupdatedwm6aft.newWriter().withWriter { it << lastupdatedwm6 }
  24. println lastupdatedwm6aft.text
  25. WebUI.delay(2)
  26. // 在 drugsync Pull 之前读取数据
  27. def lastupdatedwm6bef = new File(RunConfiguration.getProjectDir() + "/Data Files/lastupdatedwm6bef.txt")
  28. Date data1 = sdf.parse(lastupdatedwm6bef.text);
  29. Date data2 = sdf.parse(lastupdatedwm6aft.text);
  30. long diffInMillies = Math.abs(data2.getTime() - data1.getTime());
  31. long minute_millis_2 = 2 * 60 * 1000;
  32. long diffTime = minute_millis_2 - diffInMillies;
  33. assertTrue(diffTime > 0);

希望这些翻译对您有所帮助。如果您有任何其他问题,请随时问我。

英文翻译

I want to have assertion to make sure the data retrieved is within

  1. print data1
    2020-03-16 09:08:49

  2. print data2
    2020-03-16 09:09:15

  3. assertThat time gap between data1 and data2 is not more than 2 minute then Passed.

I have sample codes, is this the best way?
any suggestion, please comment.

  1. //data1
  2. Date data1 = new Date();
  3. //data2
  4. Date data2 = new Date();
  5. //assert
  6. assertThat(data1, DateMatchers.within(2, ChronoUnit.MINUTES, data2 ));

latest script

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

  1. import static org.assertj.core.api.Assertions.*
  2. import java.sql.*
  3. import java.text.SimpleDateFormat
  4. import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
  5. import internal.GlobalVariable as GlobalVariable
  6. import java.text.ParseException
  7. import java.text.SimpleDateFormat
  8. import java.util.Date
  9. import com.kms.katalon.core.configuration.RunConfiguration
  10. GlobalVariable.TestIssueKey = null
  11. WebUI.delay(1)
  12. //SQL statement
  13. dbQuery2 = /SELECT * FROM drugs.sync/
  14. //Connect to PostgresSQL, global variable is stored at profile
  15. List results = CustomKeywords.&#39;test.database.getPostgresSQLResults&#39;(GlobalVariable.dbConnString2 , GlobalVariable.dbUsername2 , GlobalVariable.dbPassword2 ,GlobalVariable.dbDriver2 ,dbQuery2 )
  16. //SimpleDateFormat sdf = new SimpleDateFormat(&quot;yyyy-MM-dd&#39;T&#39;HH:mm:ss.SSS&#39;Z&#39;&quot;)
  17. SimpleDateFormat sdf = new SimpleDateFormat(&quot;yyyy-mm-dd hh:mm:ss&quot;, Locale.ENGLISH)
  18. String date = sdf.format(new Date())
  19. //print the &quot;lastupdatedwm6&quot; column for PULL
  20. String lastupdatedwm6 = results.get(0).get(&#39;lastupdatedwm6&#39;)
  21. //store the lastupdatedwm6 to file
  22. def lastupdatedwm6aft = new File(RunConfiguration.getProjectDir() + &quot;/Data Files/lastupdatedwm6aft.txt&quot;)
  23. lastupdatedwm6aft.newWriter().withWriter { it &lt;&lt; lastupdatedwm6 }
  24. println lastupdatedwm6aft.text
  25. WebUI.delay(2)
  26. //Read data before drugsync Pull
  27. def lastupdatedwm6bef = new File(RunConfiguration.getProjectDir() + &quot;/Data Files/lastupdatedwm6bef.txt&quot;)
  28. Date data1 = sdf.parse(lastupdatedwm6bef.text);
  29. Date data2 = sdf.parse(lastupdatedwm6aft.text);
  30. long diffInMillies = Math.abs(data2.getTime() - data1.getTime());
  31. long minute_millis_2 = 2 * 60 * 1000;
  32. long diffTime = minute_millis_2 - diffInMillies;
  33. assertTrue(diffTime &gt; 0);

<!-- end snippet -->

答案1

得分: 0

  1. @Test
  2. public void test() throws ParseException {
  3. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss", Locale.ENGLISH);
  4. Date data1 = sdf.parse("2020-03-16 09:08:49");
  5. Date data2 = sdf.parse("2020-03-16 09:09:49");
  6. long diffInMillies = Math.abs(data2.getTime() - data1.getTime());
  7. long minute_millis_2 = 2 * 60 * 1000;
  8. long diffTime = minute_millis_2 - diffInMillies;
  9. assertTrue(diffTime > 0);
  10. }
英文翻译
  1. @Test
  2. public void test() throws ParseException {
  3. SimpleDateFormat sdf = new SimpleDateFormat(&quot;yyyy-mm-dd hh:mm:ss&quot;, Locale.ENGLISH);
  4. Date data1 = sdf.parse(&quot;2020-03-16 09:08:49&quot;);
  5. Date data2 = sdf.parse(&quot;2020-03-16 09:09:49&quot;);
  6. long diffInMillies = Math.abs(data2.getTime() - data1.getTime());
  7. long minute_millis_2 = 2 * 60 * 1000;
  8. long diffTime = minute_millis_2 - diffInMillies;
  9. assertTrue(diffTime &gt; 0);
  10. }

答案2

得分: 0

另一种方法是使用超时。

  1. // 如果测试时间超过1000毫秒,这个测试将会失败
  2. @Test(timeout=1000)
  3. public void test() {
  4. objectToTest.methodThatTakesALongTime();
  5. }
英文翻译

Another approach is to use timeout.

  1. // This will fail if the test takes more than 1000 milliseconds
  2. @Test(timeout=1000)
  3. public void test() {
  4. objectToTest.methodThatTakesALongTime();
  5. }

huangapple
  • 本文由 发表于 2020年3月16日 17:52:09
  • 转载请务必保留本文链接:https://java.coder-hub.com/60703705.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定