Read before/after test results from a file for easier formatting / test verification?
ryanseys opened this issue · 4 comments
Have you considered putting the expected test results in separate files and reading them in and comparing them to the output? This might be easier than writing concatenation code like this little ditty:
".OtherActivity\">\n" +
" </activity>\n" +
"\n" +
" <activity android:name=\"com.example.android.custom-lint-rules" +
".MainActivity\">\n" +
" <intent-filter>\n" +
which is very easy to screw up, requires a lot of trial and error, and is hard to see being correct at a quick glance.
Something like assertTrue(runLint("test1before.txt").equals(readFile("test1after.txt"))); would making writing and verifying tests much faster and easier to maintain.
The tests were originally written that way in separate files, but the current standard in lint-checks is to have test input and output inlined into the test (See other lint-check tests). Having separate test resource files was problematic. For example, setting the path of the test files is a bit of work and having separate files that have lint issues will cause the project to fail to build because of these issues (very meta).
Some things that help:
- Syntax highlighting (via AS's
@Languageannotation) - Copy concatenated string to clipboard via quickfix (Alt - Enter)
- Pasting plain text into a string will escape and format the text
So although it's a little less readable, it's very convenient and is relatively easy to modify via copy and paste.
Having the file inline also makes it easy to read and understand what a test is doing. In the other case, where the file is external, you'd have to open multiple files to understand what is under test.
@ryanseys
Regarding the input:
You could specify the "input" inline (like demonstrated here) and use lintProject() or in an external resource file and use lintFile().
Regarding the output:
The lint-tests library compiles all Warnings to a string. If you need a more detailed assertion check my lint-junit-rule.
Besides allowing you to test your custom Lint rules using JUnit4 it allows a very detailed assertion by exposing the reported Warnings:
@Rule public Lint lint = new Lint(new MyCustomRule(), MyCustomRule.ISSUE);
@Test
public void test() throws Exception {
List<Warning> warnings = lint.files("AndroidManifest.xml", "res/values/string.xml");
assertThat(warnings).extracting("file.name", "line", "message")
.containsExactly(
tuple("AndroidManifest.xml", 8, "MyCustomRule warning message."),
tuple("string.xml", 14, "MyCustomRule warning message."));
}