E81015: test construct only allowed in dev source files
What went wrong
A test construct (@Test, assert, assertThrows, assertDoesNotThrow) was used in a source file that is not a development source. Test constructs are only allowed in files located in a 'dev/' subdirectory or matching the QA####_dev_ naming convention.
Worked examples
Compilable examples demonstrating this error and the correct fix:
- ek9 -q 767 (What is the dev context and how does it relate to testing...)
How to fix it
- Move the file to a 'dev/' subdirectory: src/dev/tests.ek9
- Or rename using the QA companion pattern: QA0001_dev_validators.ek9
- If this is production validation, use 'require' instead of 'assert': require input?
[ASSERT vs REQUIRE]
assert → Test validation only (dev sources) require → Production precondition (any source, throws if violated)
Rationale
EK9 enforces strict separation between test and production code at compile time. Test constructs in production sources indicate confused intent: either the code should use 'require' for production preconditions, or the file belongs in the dev/ directory. Catching this early (at the point of use) prevents developers from writing significant test code in the wrong location and discovering the problem only during call graph analysis.
See also
E81011: Assertion not reachable from @Test program (orphan assertion) E81012: Assertion reachable from non-test program (production assertion) E81007: @Test program with no assertions
Examples
Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.
Fails with E81015: test construct only allowed in dev source files
#!ek9 defines module error.e81015.bad defines function validateInput() -> input as Integer <- valid as Boolean? assert input? valid: true defines program MainProgram() stdout <- Stdout() stdout.println("Hello")
Fixed: Check an input in production code with 'require'; keep 'assert' for dev/ tests
#!ek9 defines module error.e81015.good defines function validateInput() -> input as Integer <- valid as Boolean? require input? valid: true defines program MainProgram() stdout <- Stdout() stdout.println("Hello")
Questions and answers
These Q&A entries show E81015 and its fix: