Wiki source code of Testing

Version 138.8 by Vincent Massol on 2018/11/25 16:24

Show last authors
1 {{box cssClass="floatinginfobox" title="**Contents**"}}
2 {{toc/}}
3 {{/box}}
4
5 There are various types of tests and everyone has its own terminology. You'll find below the XWiki terminology and best practices related to testing.
6
7 Check the [[general development flow>>dev:Community.DevelopmentPractices||anchor="HGeneralDevelopmentFlow"]] to understand better how testing fits in the larger picture.
8
9 = Testing Strategy =
10
11 Here's the general testing methodology used by the XWiki project:
12
13 * Code committed must have associated automated tests. There's [[a check in the build>>Community.Building||anchor="HAutomaticChecks"]] (in the ##quality## profile) to ensure that committed code do not reduce the total test coverage for that module. The CI runs the ##quality## profile for each commit and fails the build if the test coverage is reduced.
14 ** In addition we also check for Test quality by computing Mutation Testing scores for all tests and fail the build if the global quality in a given module is reduced by a new commit (this is checked by some CI jobs every day).
15 * Developers run unit and functional tests on their machines daily and frequently.
16 * Unit and functional tests are executed by the CI, at each commit.
17 * Most automated functional tests are currently running in a single environment (HSQLDB/Jetty/Firefox). However, we've started to use Docked-based testing to test automatically using several configurations and are migrating more and more tests to that. There are jobs in XWiki's CI that execute tests in all configurations regularly (not at each commit though but as much as possible).
18 ** Manual tests are also executed at each release by dedicated contributors and they manually execute tests that are not covered by the automated tests.
19 * Performance tests are executed manually several times per year (usually for LTS releases and sometimes for stable releases too).
20 * Responsiveness tests (e.g. verify that XWiki displays fine on a Mobile) are currently executed manually from time to time by dedicated contributors.
21 * Accessibility (WCAG) tests are performed automatically at each commit.
22 * HTML5 validity tests are performed automatically at each commit.
23
24 = Unit Testing =
25
26 == Java Unit Testing ==
27
28 See [[Java Unit Testing>>Community.Testing.JavaUnitTesting.WebHome]]
29
30 == JavaScript Unit Testing ==
31
32 * These are **tests that do not rely on the DOM**, written as "behavioral specifications", using the [[Jasmine>>http://pivotal.github.io/jasmine/]] test framework.
33 * In development mode, you can start the test runner process by running {{code}}mvn jasmine:bdd{{/code}} in your command line. This will start a test runner at ##http:~/~/localhost:8234##, that will run all tests in the ##src/test/javascript## directory. Write your tests there and hit refresh to see the results directly in your browser.
34 * For tests that need a DOM, see [[Functional Testing>>||anchor="HFunctionalTesting"]]
35
36 == Java Rendering Testing ==
37
38 We have a special framework for making it easy to write Rendering tests, see the [[Rendering Testing Framework>>rendering:Main.Extending||anchor="HAddingTests"]]
39
40 == XAR Testing ==
41
42 See [[XAR Unit Testing>>Community.Testing.XARUnitTesting.WebHome]]
43
44 = Functional Testing =
45
46 A functional test requires a running XWiki instance.
47
48 == Functional UI Testing ==
49
50 We now have 3 frameworks for running GUI tests:
51
52 * One based on Docker (and using Selenium 3+). This is now the recommended framework to use for new tests.
53 * One based on Selenium2/Webdriver which is now deprecated and shouldn't be used. We encourage committers to port tests written for it to the Selenium3 framework. Especially when committers bring modification to the old tests we encourage them to rewrite the tests as new Selenium3 tests.
54 * A last one based on Selenium1 which is also deprecated and shouldn't be used. We encourage committers to port tests written for it to the Selenium3 framework. Especially when committers bring modification to the old tests we encourage them to rewrite the tests as new Selenium3 tests.
55
56 {{id name="HSelenium3-basedFramework"/}}
57
58 === Docker-based Testing ===
59
60 This is currently the official way to write UI functional tests.
61
62 See [[DockerTesting]].
63
64 === Selenium2-based Framework ===
65
66 Using:
67
68 * To debug a test simply start XE somewhere and then debug your JUnit tests as a normal JUnit test in your IDE.
69 ** Note that functional test Maven modules will create an XWiki instance in ##target/xwiki## thanks to the execution of the XWiki Packager plugin, so it's simpler to start this XWiki instance when debugging.
70 * In order to debug more easily flickering tests you can simply add the {{{@Intermittent}}} annotation on your test method and it'll be executed 100 times in a row (you can also specify {{{@Intermittent(repetition=N)}}}to repeat it N times). This is achieved thanks to the [[Tempus Fugit>>http://tempusfugitlibrary.org/documentation/junit/intermittent/]] framework that we've integrated.
71 * To run a specific test, pass the ##pattern## property (it's a regex on the test class name) as in: ##mvn install -Dpattern=TestClass#testName## (this will run the ##testName## test from ##TestClass##)
72 ** More details on the usage of the ##pattern## property and examples can be found in the [[documentation of the XWikiExecutorTestMethodFilter class>>https://github.com/xwiki/xwiki-platform/blob/master/xwiki-platform-core/xwiki-platform-test/xwiki-platform-test-integration/src/main/java/org/xwiki/test/integration/XWikiExecutorTestMethodFilter.java#L25]].
73 * To run the tests on your own running instance (instead of letting Maven close your instance and start a fresh one), use ##-Dxwiki.test.verifyRunningXWikiAtStart=true##. It could be useful to verify that you have not broken the tests on your instance before committing your changes.
74 * By default the Firefox browser will be used but if you wish to run with another browser just pass the ##browser## parameter as in:
75 ** Firefox (default): ##-Dbrowser=*firefox##
76 ** Internet Explorer: ##-Dbrowser=*iexplore##
77 ** Chrome: ##-Dbrowser=*chrome##
78 ** PhantomJS: ##-Dbrowser=*phantomjs##
79 * You may reach a compatibility problem between the version of the browser and the version of Selenium. For example, at the moment, Firefox 32.0 is required. You may install it on your computer and refer to it with ##-Dwebdriver.firefox.bin="/path/to/your/firefox-32.0"##
80 * We check for pages that require Programming Rights automatically by bundling a Listener component that listens to the ##ScriptEvaluatingEvent## event and that drops Programming Rights, in an effort to make the tests fail and so that the developer can notice he requires PR and fix that. In some cases where it's necessary, this can be disabled by setting the configuration {{code}}< testProgrammingRights>false</testProgrammingRights>{{/code}} in the configuration of the Package Mojo, and/or by using a System Property to control where the check is performed, e.g. {{code}}<xwikiPropertiesAdditionalProperties>test.prchecker.excludePattern=.*:XWiki\.XWikiPreferences</xwikiPropertiesAdditionalProperties>{{/code}}. {{info}}Since XWiki 9.8RC1{{/info}}
81 * If the ##xwiki.test.startXWiki## system property is set to ##true## then the test itself will start/stop XWiki. If set to ##false## then it's then the responsibility of the build to start/stop XWiki. Useful when starting.stopping XWiki inside a Docker container handled by the Maven build for example. {{info}}Since XWiki 10.0{{/info}}
82
83 ==== Best practices ====
84
85 * Tests are located in ##xwiki-platform## inside the module that they are testing. Note that in the past we were putting functional tests in ##[[xwiki-platform-distribution/xwiki-platform-distribution-flavor/xwiki-platform-distribution-flavor-test>>https://github.com/xwiki/xwiki-platform/tree/master/xwiki-platform-distribution/xwiki-platform-distribution-flavor/xwiki-platform-distribution-flavor-test]]## but we have started to move them inside the specific modules they are testing.
86 * Name the tests using ##<prefix>IT.java##.
87 * Use a ##AllITs.java## test suite to ensure that XWiki is started/stopped only once for all tests in the module:(((
88 {{code language="java"}}
89 @RunWith(PageObjectSuite.class)
90 public class AllITs
91 {
92 }
93 {{/code}}
94 )))
95 * Use the ##maven-failsafe-plugin## for functional UI tests:(((
96 {{code language="xml"}}
97 <plugin>
98 <groupId>org.apache.maven.plugins</groupId>
99 <artifactId>maven-failsafe-plugin</artifactId>
100 </plugin>
101 {{/code}}
102 )))
103 * Locate tests in ##src/test/java## (i.e the default maven location for tests)
104 * Tests should be written using the Page Objects Pattern:
105 ** [[Original article from Simon Steward>>https://code.google.com/p/selenium/wiki/PageObjects]]
106 ** [[Article from Martin Fowler>>http://martinfowler.com/bliki/PageObject.html]]
107 * Since functional tests take a long time to execute (XWiki to start, Browser to start, navigation, etc) it's important to write tests that execute as fast as possible. Here are some tips:
108 ** Write scenarios (i.e. write only a few test methods, even only one if you can so that you can have a single fixture) instead of a lot of individual tests as you'd do for unit tests.
109 ** Use ##getUtil()## to perform quick actions that are not part of what you wish to test (i.e that are part of the test fixture). For example to create a page you need in your fixture, write:(((
110 {{code language="java"}}
111 getUtil().createPage(...);
112 {{/code}}
113 )))instead of(((
114 {{code language="java"}}
115 WikiEditPage wep = new WikiEditPage();
116 wep.switchToEdit(SPACE_NAME, DOC_NAME);
117 wep.setTitle(DOC_TITLE);
118 wep.setContent(CONTENT);
119 ViewPage vp = wep.clickSaveAndView();
120 {{/code}}
121 )))
122 ** Your Maven module for test may depend on a specific profile (as a best practice, you can use ##integration-tests##). Doing this, it will be built only when specifically asked with ##-Pintegration-tests## (see below for an example of the ##pom.xml##)(((
123 {{code language="xml"}}
124 <profiles>
125 <profile>
126 <id>integration-tests</id>
127 <modules>
128 <module>application-task-test</module>
129 </modules>
130 </profile>
131 </profiles>
132 {{/code}}
133 )))
134 * {{warning}}Never, ever, wait on a timer!{{/warning}} Instead wait on elements.
135 * If you need to create, update or delete a page during your tests, use a space name specific to your test scenario with ##getTestClassName()##. For example:(((
136 {{code language="java"}}
137 getUtil().createPage(getTestClassName(), "Page", "Content", "Title");
138 {{/code}}
139 )))
140 * Tests must not depend on one another. In other words, it should be possible to execute tests in any order and running only one test class should work fine.
141 * Tests that need to change existing configuration (e.g. change the default language, set specific rights, etc) must put back the configuration as it was. This is true only in flavor tests or when several functional tests of different domains are executed one after another. However functional tests located in xwiki-platform specific modules are only running the tests for their module and thus it's not important, and saves times, if they don't clean up.
142 * Tests are allowed to create new documents and don't need to remove them at the end of the test.
143
144 Examples of functional tests:
145
146 * {{scm path="xwiki-platform-core/xwiki-platform-activeinstalls/xwiki-platform-activeinstalls-test"}}Active Installs Application Functional Tests{{/scm}}
147 * {{scm path="xwiki-platform-core/xwiki-platform-administration/xwiki-platform-administration-test"}}Administration Application Functional Tests{{/scm}}
148 * {{scm path="xwiki-platform-core/xwiki-platform-faq/xwiki-platform-faq-test"}}FAQ Application Functional Tests{{/scm}}
149 * {{scm path="xwiki-platform-core/xwiki-platform-ircbot/xwiki-platform-ircbot-test"}}IRCBot Application Functional Tests{{/scm}}
150 * {{scm path="xwiki-platform-core/xwiki-platform-linkchecker/xwiki-platform-linkchecker-test"}}Link Checker Application Functional Tests{{/scm}}
151 * {{scm path="xwiki-platform-core/xwiki-platform-panels/xwiki-platform-panels-test"}}Panels Application Functional Tests{{/scm}}
152
153 === The Office Importer tests ===
154
155 In XWiki 7.3, we have introduced in ##xwiki-platform## some functional tests for the [[extensions:Extension.Office Importer Application]]. To enable them, you need to enable the profile ##office-tests##. An OpenOffice (or LibreOffice) server is needed on your system. You might also need to set an environment variable that points to the office home path, if not standard. This variable is called ##XWIKI_OFFICE_HOME## and can be set like this:
156
157 (((
158 {{code language="sh"}}
159 ## For Unix systems:
160 export XWIKI_OFFICE_HOME="/opt/libreoffice3.6"
161 {{/code}}
162 )))
163
164 You should set this environment variable in your CI agents.
165
166 === Old Selenium1-based Framework ===
167
168 * We were using Selenium RC to perform functional tests for GUI. We had created some JUnit extension to easily write Selenium tests in Java.
169 ** Existing tests can be found in ##[[xwiki-enteprise/xwiki-enterprise-test/xwiki-enterprise-test-selenium>>https://github.com/xwiki/xwiki-enterprise/tree/master/xwiki-enterprise-test/xwiki-enterprise-test-selenium]]##
170 * To run these tests on your local machine go to ##xwiki-enteprise/xwiki-enterprise-test/xwiki-enterprise-test-selenium## and type ##mvn install##.
171 * To run a specific test, pass the ##pattern## property as in: ##mvn install -Dpattern=DeletePageTest## (this will run the ##DeletePageTest## - Note that you don't have to specify the extension). In addition if you wish to execute only a specific method from a Test Case class, you can pass the ##patternMethod## property as in: ##mvn install -Dpattern=DeletePageTest -DpatternMethod=testDeletePageCanDoRedirect##.
172 * To enable debug output from the selenium server start maven with the ##-Ddebug=true## switch and then all messages from the selenium server are saved to: ##xwiki-enteprise/xwiki-enterprise-test/xwiki-enterprise-test-selenium/target/selenium/server.log##.
173 * To debug a functional Selenium test in your favourite Java IDE go in ##xwiki-enteprise/xwiki-enterprise-test/xwiki-enterprise-test-selenium## and run maven with ##-Dmaven.surefire.debug##. Maven will wait till you connect your IDE to the running JVM on port 5005. You can put a breakpoint in your IDE and debug the test.
174
175 === Browser version ===
176
177 Currently we only run our functional tests on the ##Firefox## browser.
178 The browser you use for functional tests needs to match the selenium version, otherwise unpredictable results may occur.
179
180 * The current ##Selenium## version we are using is ##2.44.0##
181 ** (valid for 18.March.2015. Actual version used can be verified [[here>>https://github.com/xwiki/xwiki-commons/blob/master/pom.xml]] under the ##selenium.version## property)
182 * The ##Firefox## version we use in you continuous integration agents is ##32.0.1##.
183 ** (valid for 18.March.2015. Ask on the [[list>>dev:Community.MailingLists]] or on [[IRC>>dev:Community.IRC]] for the actual used version since it's not publicly verifiable)
184 ** To determine browser compatibility with the Selenium version used, scan [[Selenium's changelog>>https://code.google.com/p/selenium/source/browse/java/CHANGELOG]] and look for entries like "* Updating Native events to support Firefox 24, 31, 32 and 33". That shows the supported browser version for the particular Selenium version.
185
186 If you wish to run tests with the exact configuration as XWiki's [[Continous Integration server>>http://ci.xwiki.org]] uses, you need to install and use locally the same Firefox version. To do so, you have to:
187
188 1. [[Download>>https://ftp.mozilla.org/pub/mozilla.org/firefox/releases/]] the corresponding Firefox release
189 1. Unzip it locally
190 1. Use the ##webdriver.firefox.bin## java system property to specify the location of your firefox version
191 11. Depending on how you are starting the functional tests, you`d have to either add the system property in your maven build (surefire plugin configuration) or in your IDE (run configuration)
192 11. Read [[Selenium's FirefoxDriver documentation>>https://code.google.com/p/selenium/wiki/FirefoxDriver]] for more information and options
193
194 = XHTML, CSS & WCAG Validations =
195
196 * We are using JUnit to validate that all XWiki pages produce valid XHTML.
197 ** Existing tests can be found in ##enterprise/distribution-test/misc-tests/##
198 * [[WCAG Testing Strategy>>WCAGTesting]]
199 * CSS validation must be verified manually using the [[W3C validator>>http://jigsaw.w3.org/css-validator/]]. See [[XHTML & CSS Coding Style>>XhtmlCssCodeStyle]].
200
201 = Performance Testing =
202
203 * These are memory leakage tests, load tests and speed of execution tests.
204 * They are performed manually and in an ad hoc fashion for now. They are executed for some stable versions and for all super stable versions.
205 * See [[Methodology and reports>>test:Performances.WebHome]].
206
207 See the [[Profiling topic>>Profiling]] for details on how to use a profiler for detecting performance issues.
208
209 = Manual testing =
210
211 {{info}}
212 Here's the spirit we'd like to have from the XWiki Manual Testing Team: [[The Black Team>>http://www.t3.org/tangledwebs/07/tw0706.html]].
213 {{/info}}
214
215 Besides automated testing, ensuring software quality requires that features be tested by actual users (see [[Manual testing>>http://en.wikipedia.org/wiki/Manual_testing]]). In order to manage manual testing, a [[test plan>>http://en.wikipedia.org/wiki/Test_plan]] is required.
216
217 * [[Manual test reports>>test:Main.WebHome]]
218 * [[Manual Test Strategy>>ManualTestStrategy]]
219 * [[Manual Test Reports>>xwiki:TestReports.WebHome]]
220
221 = Tools Reference =
222
223 We use the following tools in our automated tests:
224
225 * [[JUnit>>https://junit.org/junit5/]]: test framework
226 * [[Mockito>>https://site.mockito.org/]]: mocking environment of unit test to isolate it
227 * [[Hamcrest>>http://hamcrest.org/JavaHamcrest/]]: extra assertions beyond what JUnit provides
228 * [[GreenMail>>http://www.icegreen.com/greenmail/]]: for testing email sending
229 * [[WireMock>>http://wiremock.org/]]: for simulating HTTP connections to remote servers
230 * [[Selenium>>https://seleniumhq.github.io/docs/]]: for functional UI test to simulate a user interacting with XWiki
231 * [[TestContainers>>https://www.testcontainers.org/]]: Docker-based functional testing
232 * [[JMeter>>http://jmeter.apache.org/]]: for performance tests
233 * [[Dumbbench>>https://github.com/tsee/dumbbench]]: for manual performance tests
234
235 = Test Coverage =
236
237 See the [[TestCoverage]] page.

Get Connected