-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Added initial tests for ExportFormat #1010
Conversation
MetaData metaData = new MetaData(); | ||
exportFormat.performExport(db, metaData, filename, charset, entries); | ||
} catch (IOException e) { | ||
Assert.fail("Exception caught: " + e.toString() + e.getMessage()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would probably be more readable code if you just skip the try-catch here. The tests will fail anyway if the method is terminated by an exception and we will get all the exception info we need. There is no need to explicitly build an exception string as you do here.
Some code quality tools might unnecessarily complain about missing assertions, though...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lenhard That should be a general rule in my opinion, not to catch Exceptions ins Tests.
Edit// At the moment there are tests that fail but omit stacktrace. I'lll create a new issue for this
Mhh....I am missing an assertion in these tests, i.e. reading the exported file and checking that it is empty. |
I have changed the code based on all(?) comments. Major implications:
(This shows that adding a quite crappy initial test can lead to good things. :-)) |
43089f5
to
4a6f0a4
Compare
As the export is now not performed for empty databases (entries lists), as a side effect, the coverage is more accurate now as well. Earlier it increased quite a bit, but no actual testing was done (well, apart from that the code executed without any errors). |
…ignored BibDatabaseWriter change
4a6f0a4
to
d316463
Compare
@Test | ||
public void testExportingEmptyDatabaseYieldsEmptyFile() throws Exception { | ||
BibDatabase database = new BibDatabase(); | ||
for (Map.Entry<String, IExportFormat> exportFormats : ExportFormats.getExportFormats().entrySet()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use parametrized tests instead of the for loop. Then also the the string argument to assertEquals is no longer needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any good pointer/example? (It seemed like it should be possible somehow...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/junit-team/junit4/wiki/Parameterized-tests and the tests in FormatterTests
This looks way better then the initial PR. Good job! |
Not obvious that Parameterized is executed before BeforeClass, but once I figured that out... |
61962f8
to
b9bf04a
Compare
public void setUp() { | ||
Globals.prefs = JabRefPreferences.getInstance(); | ||
ExportFormats.initAllExports(); | ||
@BeforeClass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think before is preferable to beforeclass since then every test gets a fresh database and the potentially modifications of the previous test do not influence the next test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK! I do not fully understand the difference and usually use Before. Now I just mimicked FormatterTest to see if it helped (but the problem was not related to that).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And regarding providing an actual database, the current method is not the best approach (with statics). However, it seems like the tests needs to be rather specific anyway for the different export formats so maybe they can't be parameterized in a good way anyhow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I second this. Every export format should have its own test class, which tests the actually export.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw, is there a way to add a test which is not included in the coverage check? It would make sense to try to export a non-empty database for every export format just to make sure that no exceptions are thrown, but probably one wouldn't like to include these test in the coverage report...
LGTM |
I think this is probably as far as we get right now and that the code is quite OK since it is rather general and only tests what is actually tested (or how to formulate it...). |
Really trivial and early test, but could maybe avoid bugs like #1001 to happen (and form a foundation to extend on).