-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Adding new timestamp field to event and json report #1588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
226ed70
a40796f
2a78ce7
6d4b5bb
bb39126
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ public interface EventBus extends EventPublisher { | |
|
|
||
| Long getTime(); | ||
|
|
||
| Long getElapsedTimeMillis(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, but There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. its the elapsed time since epoch in millis so i named it that way. But i ll update the name no problem. |
||
|
|
||
| void send(Event event); | ||
|
|
||
| void sendAll(Iterable<Event> queue); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,14 @@ | ||
| package io.cucumber.core.plugin; | ||
|
|
||
| import io.cucumber.core.api.event.HookTestStep; | ||
| import io.cucumber.core.api.event.HookType; | ||
| import io.cucumber.core.api.event.PickleStepTestStep; | ||
| import io.cucumber.core.api.event.Result; | ||
| import io.cucumber.core.api.event.TestCase; | ||
| import io.cucumber.core.api.event.TestStep; | ||
| import io.cucumber.core.api.event.EmbedEvent; | ||
| import io.cucumber.core.api.event.EventHandler; | ||
| import io.cucumber.core.api.event.EventListener; | ||
| import io.cucumber.core.api.event.EventPublisher; | ||
| import io.cucumber.core.api.event.TestCaseStarted; | ||
| import io.cucumber.core.api.event.TestRunFinished; | ||
| import io.cucumber.core.api.event.TestSourceRead; | ||
| import io.cucumber.core.api.event.TestStepFinished; | ||
| import io.cucumber.core.api.event.TestStepStarted; | ||
| import io.cucumber.core.api.event.WriteEvent; | ||
| import java.time.Instant; | ||
| import java.time.LocalDateTime; | ||
| import java.time.ZoneId; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import gherkin.ast.Background; | ||
| import gherkin.ast.Feature; | ||
| import gherkin.ast.ScenarioDefinition; | ||
|
|
@@ -29,11 +22,22 @@ | |
| import gherkin.pickles.PickleString; | ||
| import gherkin.pickles.PickleTable; | ||
| import gherkin.pickles.PickleTag; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import io.cucumber.core.api.event.EmbedEvent; | ||
| import io.cucumber.core.api.event.EventHandler; | ||
| import io.cucumber.core.api.event.EventListener; | ||
| import io.cucumber.core.api.event.EventPublisher; | ||
| import io.cucumber.core.api.event.HookTestStep; | ||
| import io.cucumber.core.api.event.HookType; | ||
| import io.cucumber.core.api.event.PickleStepTestStep; | ||
| import io.cucumber.core.api.event.Result; | ||
| import io.cucumber.core.api.event.TestCase; | ||
| import io.cucumber.core.api.event.TestCaseStarted; | ||
| import io.cucumber.core.api.event.TestRunFinished; | ||
| import io.cucumber.core.api.event.TestSourceRead; | ||
| import io.cucumber.core.api.event.TestStep; | ||
| import io.cucumber.core.api.event.TestStepFinished; | ||
| import io.cucumber.core.api.event.TestStepStarted; | ||
| import io.cucumber.core.api.event.WriteEvent; | ||
|
|
||
| public final class JSONFormatter implements EventListener { | ||
| private String currentFeatureFile; | ||
|
|
@@ -47,6 +51,7 @@ public final class JSONFormatter implements EventListener { | |
| private final Gson gson = new GsonBuilder().setPrettyPrinting().create(); | ||
| private final NiceAppendable out; | ||
| private final TestSourcesModel testSources = new TestSourcesModel(); | ||
| private DateTimeFormatter dateTimeFormatter; | ||
|
|
||
| private EventHandler<TestSourceRead> testSourceReadHandler = new EventHandler<TestSourceRead>() { | ||
| @Override | ||
|
|
@@ -118,7 +123,7 @@ private void handleTestCaseStarted(TestCaseStarted event) { | |
| featureMaps.add(currentFeatureMap); | ||
| currentElementsList = (List<Map<String, Object>>) currentFeatureMap.get("elements"); | ||
| } | ||
| currentTestCaseMap = createTestCase(event.testCase); | ||
| currentTestCaseMap = createTestCase(event); | ||
| if (testSources.hasBackground(currentFeatureFile, event.testCase.getLine())) { | ||
| currentElementMap = createBackground(event.testCase); | ||
| currentElementsList.add(currentElementMap); | ||
|
|
@@ -187,8 +192,13 @@ private Map<String, Object> createFeatureMap(TestCase testCase) { | |
| return featureMap; | ||
| } | ||
|
|
||
| private Map<String, Object> createTestCase(TestCase testCase) { | ||
| private Map<String, Object> createTestCase(TestCaseStarted event) { | ||
| Map<String, Object> testCaseMap = new HashMap<String, Object>(); | ||
|
|
||
| testCaseMap.put("start_timestamp", getDateTimeFromTimeStamp(event)); | ||
|
|
||
| TestCase testCase = event.getTestCase(); | ||
|
|
||
| testCaseMap.put("name", testCase.getName()); | ||
| testCaseMap.put("line", testCase.getLine()); | ||
| testCaseMap.put("type", "scenario"); | ||
|
|
@@ -212,6 +222,11 @@ private Map<String, Object> createTestCase(TestCase testCase) { | |
| return testCaseMap; | ||
| } | ||
|
|
||
| private String getDateTimeFromTimeStamp(TestCaseStarted event) { | ||
| LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(event.getElapsedTimiMillis()), ZoneId.systemDefault()); | ||
| return localDateTime.format(dateTimeFormatter == null ? DateTimeFormatter.ISO_DATE_TIME : dateTimeFormatter); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the JSON output the format should be fixed predictable. So I would think the zone should always be UTC. Using a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And probably good to bear in mind that v4 is still using Java 7. So you can't use the fancy Date stuff yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And well Java 7 puts away both the localdatetime and zonedatetime options away. And shouldn't it just be the default zone of the PC running the TCs? In case they use it to see the timestamp of the TC run in the report. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kept the dateTimeFormatter optional for the sake of flexibility in case we don't want the full time in some cases (we just want the date, etc). Its actually also being used in the JUnits to bypass the 'dynamic generation' issue, for eg in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer not to depend on the default timezone of the machine running the tests, as this might be either a local machine with locally running application, or a CI server (i.e. Jenkins) running tests against an actual environment; where the CI server and the test environment might not have the same system datetime / timezone. Being able to specify it is preferable inho There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes that makes sense. I can change it to always be UTC in that case. But do u mean allow the user to specify what timezone to use before running the tests? From cucumber options or the like? |
||
| } | ||
|
|
||
| private Map<String, Object> createBackground(TestCase testCase) { | ||
| TestSourcesModel.AstNode astNode = testSources.getAstNode(currentFeatureFile, testCase.getLine()); | ||
| if (astNode != null) { | ||
|
|
@@ -378,4 +393,9 @@ private Map<String, Object> createResultMap(Result result) { | |
| } | ||
| return resultMap; | ||
| } | ||
|
|
||
| public void setDateTimeFormatter(DateTimeFormatter dateTimeFormatter) { | ||
| this.dateTimeFormatter = dateTimeFormatter; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,12 +2,18 @@ | |
|
|
||
| public interface TimeService { | ||
| long time(); | ||
| long elapsedTimeMillis(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
|
|
||
| TimeService SYSTEM = new TimeService() { | ||
| @Override | ||
| public long time() { | ||
| return System.nanoTime(); | ||
| } | ||
|
|
||
| @Override | ||
| public long elapsedTimeMillis() { | ||
| return System.currentTimeMillis(); | ||
| } | ||
| }; | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.