Skip to content

Commit 02dba83

Browse files
wip
1 parent fe4922b commit 02dba83

3 files changed

Lines changed: 59 additions & 82 deletions

File tree

README.md

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
clojure.test.generative
22
========================================
33

4-
Test data generation and execution harness. Very early days.
5-
This API will change. You have been warned.
6-
4+
Generative test runner.
75

86
Releases and Dependency Information
97
========================================
108

11-
Latest stable release: 0.1.4
9+
Latest stable release: 0.4.0
1210

1311
* [All Released Versions](http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.clojure%22%20AND%20a%3A%22test.generative%22)
1412

1513
* [Development Snapshot Versions](https://oss.sonatype.org/index.html#nexus-search;gav~org.clojure~test.generative~~~)
1614

1715
[Leiningen](https://github.com/technomancy/leiningen) dependency information:
1816

19-
[org.clojure/test.generative "0.1.4"]
17+
[org.clojure/test.generative "0.4.0"]
2018

2119
[Maven](http://maven.apache.org/) dependency information:
2220

2321
<dependency>
2422
<groupId>org.clojure</groupId>
2523
<artifactId>test.generative</artifactId>
26-
<version>0.1.4</version>
24+
<version>0.4.0</version>
2725
</dependency>
2826

2927

@@ -41,18 +39,6 @@ and a validator:
4139
To generate test data, see the fns in the generators namespace. Note
4240
that these functions shadow a bunch of clojure.core names.
4341

44-
You can run clojure.test and clojure.test.generative tests together
45-
from the c.t.g. runner:
46-
47-
(require '[clojure.test.generative.runner :as runner])
48-
(runner/-main "src/test/clojure" "src/examples/clojure")
49-
50-
Assertion support is currently minimal. There is an `is` macro,
51-
similar to clojure.test's, that provides rudimentaty contextual
52-
reporting. You can also use plain assertions, or clojure.test
53-
validation forms such as `is` and `are`, or any other forms that throw
54-
exception on failure. No contextual reporting for these yet.
55-
5642
You can configure the runner with Java system properties:
5743

5844
<table>
@@ -65,14 +51,8 @@ You can configure the runner with Java system properties:
6551
<tr>
6652
<td>clojure.test.generative.msec</td><td>Desired test run duration</td>
6753
</tr>
68-
<tr>
69-
<td>clojure.test.generative.handlers</td><td>Comma-delimited list of handlers</td>
70-
</tr>
7154
</table>
7255

73-
The default handler prints test run to stdout, but others could
74-
e.g. put test events in a database.
75-
7656
Developer Information
7757
========================================
7858

@@ -84,20 +64,6 @@ Developer Information
8464

8565
* [Compatibility Test Matrix](http://build.clojure.org/job/test.generative-test-matrix/)
8666

87-
Change Log
88-
====================
89-
90-
* Release 0.1.5 (in development)
91-
* Can now run tests under clojure.test
92-
* Tests produce data events that can be consumed by arbitrary reporting tools
93-
* Example reporting integration with logback.
94-
* Added `is` macro with more detailed reporting than `clojure.test/is`
95-
* Removed collection based input generators. Input generators must be fns.
96-
* Removed duplicate input check. Tests can be called multiple times with same input.
97-
* Release 0.1.4 on 2012.01.03
98-
* Initial version
99-
100-
10167
Copyright and License
10268
========================================
10369

pom.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,13 @@
5959
<dependency>
6060
<groupId>org.clojure</groupId>
6161
<artifactId>tools.namespace</artifactId>
62-
<version>0.1.1</version>
62+
<version>0.2.3</version>
6363
</dependency>
6464
<dependency>
6565
<groupId>org.clojure</groupId>
6666
<artifactId>data.generators</artifactId>
6767
<version>0.1.2</version>
6868
</dependency>
69-
<dependency>
70-
<groupId>ch.qos.logback</groupId>
71-
<artifactId>logback-classic</artifactId>
72-
<version>1.0.6</version>
73-
<scope>provided</scope>
74-
</dependency>
7569
</dependencies>
7670

7771
<build>

src/main/clojure/clojure/test/generative/runner.clj

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
(def ^:private config-mapping
1919
[["clojure.test.generative.threads"
20-
[:threads]
20+
[:nthreads]
2121
read-string
2222
(max 1 (dec (.availableProcessors (Runtime/getRuntime))))]
2323
["clojure.test.generative.msec"
@@ -33,7 +33,6 @@
3333
(if (seq val)
3434
(assoc-in m path (coerce val))
3535
(assoc-in m path default))))
36-
{}
3736
config-mapping))
3837

3938
(def ^:private ^java.util.Random rnd (java.util.Random. (System/currentTimeMillis)))
@@ -43,10 +42,10 @@
4342
(locking rnd
4443
(.nextInt rnd)))
4544

46-
(defprotocol TestContainer
45+
(defprotocol Testable
4746
(get-tests [_]))
4847

49-
(extend-protocol TestContainer
48+
(extend-protocol Testable
5049
clojure.lang.Var
5150
(get-tests
5251
[v]
@@ -65,23 +64,22 @@
6564
[m] m))
6665

6766

68-
(defn find-vars-in-namespaces
67+
(defn- find-vars-in-namespaces
6968
[& nses]
7069
(when nses
7170
(reduce (fn [v ns] (into v (vals (ns-interns ns)))) [] nses)))
7271

73-
(defn find-vars-in-dirs
72+
(defn- find-vars-in-dirs
7473
[& dirs]
7574
(let [nses (mapcat #(ns/find-namespaces-in-dir (java.io.File. ^String %)) dirs)]
7675
(doseq [ns nses] (require ns))
7776
(apply find-vars-in-namespaces nses)))
7877

79-
(defn run-one
78+
(defn- run-one
8079
"Run f (presumably for side effects) repeatedly on n threads,
8180
until msec has passed or somebody throws an exception.
8281
Returns as many status maps as seeds passed in."
83-
[{:keys [test input-gen]} msec seeds]
84-
(prn) (prn test)
82+
[{:keys [test input-gen]} {:keys [msec seeds]}]
8583
(let [f (eval test)
8684
start (System/currentTimeMillis)
8785
futs (mapv
@@ -106,24 +104,24 @@
106104
seeds)]
107105
(map deref futs)))
108106

109-
(defn run-n
110-
"Run tests in parallel on nthreads, dividing msec equally between the tests."
111-
[nthreads msec tests]
112-
(mapcat #(run-one % (/ msec (count tests)) (repeatedly nthreads next-seed)) tests))
113-
114-
(defn failed?
107+
(defn- failed?
115108
"Does test result indicate a failure?"
116109
[result]
117110
(contains? result :exception))
118111

119-
(defn run-vars
120-
"Designed for interactive use. Prints results to *out* and throws
121-
on first failure encountered."
122-
[nthreads msec & test-containers]
123-
(doseq [result (run-n nthreads msec (mapcat get-tests test-containers))]
124-
(if (failed? result)
125-
(throw (ex-info "Generative test failed" result))
126-
(prn result))))
112+
(defn- run-n
113+
"Run tests in parallel on nthreads, dividing msec equally between the tests.
114+
Returns a list of maps of :iter, :seed, :test."
115+
[{:keys [nthreads msec]} tests]
116+
(mapcat #(run-one %
117+
{:msec (/ msec (count tests))
118+
:seeds (repeatedly nthreads next-seed)})
119+
tests))
120+
121+
(defn- prf
122+
"Print and flush."
123+
[s]
124+
(print s) (flush))
127125

128126
(defn dir-tests
129127
"Returns all tests in dirs"
@@ -134,22 +132,41 @@
134132
(apply find-vars-in-namespaces)
135133
(mapcat get-tests))))
136134

135+
(defn inputs
136+
"For interactive use. Returns an infinite sequence of inputs for
137+
a test."
138+
[test]
139+
((:input-gen test)))
140+
141+
(defn run
142+
"Designed for interactive use. Prints results to *out* and throws
143+
on first failure encountered."
144+
[nthreads msec & test-containers]
145+
(doseq [result (run-n {:nthreads nthreads
146+
:msec msec}
147+
(mapcat get-tests test-containers))]
148+
(if (failed? result)
149+
(throw (ex-info "Generative test failed" result))
150+
(prn result))))
151+
137152
(defn run-suite
138153
"Designed for test suite use."
139-
[{:keys [threads msec verbose]} tests]
140-
(reduce
141-
(fn [{:keys [failures iters tests]} result]
142-
(if (or verbose (:exception result))
143-
(do (prn) (prn result))
144-
(print "."))
145-
(when (:exception result)
146-
(.printStackTrace ^Throwable (:exception result)))
147-
(flush)
148-
{:failures (+ failures (if (:exception result) 1 0))
149-
:iters (+ iters (:iter result))
150-
:tests (inc tests)})
151-
{:failures 0 :iters 0 :tests 0}
152-
(run-n threads msec tests)))
154+
[{:keys [nthreads msec progress]} tests]
155+
(let [progress (or progress #(prf "."))]
156+
(reduce
157+
(fn [{:keys [failures iters tests]} result]
158+
(when (:exception result)
159+
(.printStackTrace ^Throwable (:exception result)))
160+
(if (:exception result)
161+
(prn result)
162+
(progress))
163+
{:failures (+ failures (if (:exception result) 1 0))
164+
:iters (+ iters (:iter result))
165+
:tests (+ tests (/ 1 nthreads))})
166+
{:failures 0 :iters 0 :tests 0}
167+
(run-n {:nthreads nthreads
168+
:msec msec}
169+
tests))))
153170

154171
(defn -main
155172
"Command line entry point. Calls System.exit!"

0 commit comments

Comments
 (0)