You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[Command Line Interface](#command-line-interface)
13
+
-[Internals](#internals)
14
+
5
15
6
16
## Overview
7
17
8
18
Scrapy-Autounit is an automatic test generation tool for your Scrapy spiders.
9
19
10
20
It generates test fixtures and tests cases as you run your spiders.
11
-
The test fixtures are generated from the items and requests that your spider yields, then the test cases evaluate those fixtures against your spiders' callbacks.
21
+
22
+
The fixtures are generated from the items and requests that your spider returns, then the test cases evaluate those fixtures against your spiders' callbacks.
12
23
13
24
Scrapy Autounit generates fixtures and tests per spider and callback under the Scrapy project root directory.
14
25
Here is an example of the directory tree of your project once the fixtures are created:
@@ -36,12 +47,14 @@ my_project
36
47
│ └── my_spider.py
37
48
└── scrapy.cfg
38
49
```
50
+
39
51
40
52
## Installation
41
53
42
54
```
43
55
pip install scrapy_autounit
44
56
```
57
+
45
58
46
59
## Usage
47
60
@@ -62,74 +75,92 @@ To generate your fixtures and tests just run your spiders as usual, Scrapy Autou
62
75
$ scrapy crawl my_spider
63
76
```
64
77
When the spider finishes, a directory `autounit` is created in your project root dir, containing all the generated tests/fixtures for the spider you just ran (see the directory tree example above).
65
-
If you want to **update** your tests and fixtures you only need to run your spiders again.
78
+
79
+
If you want to **update** your tests and fixtures you only need to run your spiders again or use the [`autounit update`](#autounit-update) command line tool.
66
80
67
81
### Running tests
68
82
To run your tests you can use `unittest` regular commands.
- Keep in mind that as long as `AUTOUNIT_ENABLED` is on, each time you run a spider tests/fixtures are going to be generated for its callbacks.
89
100
This means that if you have your tests/fixtures ready to go, this setting should be off to prevent undesired overwrites.
90
101
Each time you want to regenerate your tests (e.g.: due to changes in your spiders), you can turn this on again and run your spiders as usual.
102
+
For example, this setting should be off when running your spiders in Scrapy Cloud.
91
103
92
-
- Autounit uses an internal `_autounit` key in requests' meta dictionaries. Avoid using/overriding this key in your spiders when adding data to meta to prevent unexpected behaviours.
104
+
- Autounit uses an internal `_autounit_cassette` key in requests' meta dictionaries. Avoid using/overriding this key in your spiders when adding data to meta to prevent unexpected behaviours.
105
+
93
106
94
107
## Settings
95
108
96
-
**AUTOUNIT_ENABLED**
109
+
###### General
110
+
111
+
-**AUTOUNIT_ENABLED**
97
112
Set this to `True` or `False` to enable or disable unit test generation.
98
113
99
-
**AUTOUNIT_MAX_FIXTURES_PER_CALLBACK**
114
+
-**AUTOUNIT_MAX_FIXTURES_PER_CALLBACK**
100
115
Sets the maximum number of fixtures to store per callback.
101
116
`Minimum: 10`
102
117
`Default: 10`
103
118
104
-
**AUTOUNIT_SKIPPED_FIELDS**
119
+
-**AUTOUNIT_EXTRA_PATH**
120
+
This is an extra string element to add to the test path and name between the spider name and callback name. You can use this to separate tests from the same spider with different configurations.
121
+
`Default: None`
122
+
123
+
###### Output
124
+
125
+
-**AUTOUNIT_DONT_TEST_OUTPUT_FIELDS**
105
126
Sets a list of fields to be skipped from testing your callbacks' items. It's useful to bypass fields that return a different value on each run.
106
127
For example if you have a field that is always set to `datetime.now()` in your spider, you probably want to add that field to this list to be skipped on tests. Otherwise you'll get a different value when you're generating your fixtures than when you're running your tests, making your tests fail.
107
128
`Default: []`
108
129
109
-
**AUTOUNIT_REQUEST_SKIPPED_FIELDS**
110
-
Sets a list of request fields to be skipped when running your tests.
111
-
Similar to AUTOUNIT_SKIPPED_FIELDS but applied to requests instead of items.
130
+
###### Requests
131
+
132
+
-**AUTOUNIT_DONT_TEST_REQUEST_ATTRS**
133
+
Sets a list of request attributes to be skipped when running your tests.
112
134
`Default: []`
113
135
114
-
**AUTOUNIT_EXCLUDED_HEADERS**
136
+
-**AUTOUNIT_DONT_RECORD_HEADERS**
115
137
Sets a list of headers to exclude from requests recording.
116
-
For security reasons, Autounit already excludes `Authorization` and `Proxy-Authorization` headers by default, if you want to include them in your fixtures see *`AUTOUNIT_INCLUDED_AUTH_HEADERS`*.
138
+
For security reasons, Autounit already excludes `Authorization` and `Proxy-Authorization` headers by default, if you want to record them in your fixtures see *`AUTOUNIT_RECORD_AUTH_HEADERS`*.
117
139
`Default: []`
118
140
119
-
**AUTOUNIT_INCLUDED_AUTH_HEADERS**
141
+
-**AUTOUNIT_RECORD_AUTH_HEADERS**
120
142
If you want to include `Authorization` or `Proxy-Authorization` headers in your fixtures, add one or both of them to this list.
121
143
`Default: []`
122
144
123
-
**AUTOUNIT_INCLUDED_SETTINGS**
124
-
Sets a list of settings names to be recorded in the generated test case.
145
+
###### Spider attributes
146
+
147
+
-**AUTOUNIT_DONT_RECORD_SPIDER_ATTRS**
148
+
Sets a list of spider attributes that won't be recorded into your fixtures.
125
149
`Default: []`
126
150
127
-
**AUTOUNIT_EXTRA_PATH**
128
-
This is an extra string element to add to the test path and name between the spider name and callback name. You can use this to separate tests from the same spider with different configurations.
129
-
`Default: None`
151
+
-**AUTOUNIT_DONT_TEST_SPIDER_ATTRS**
152
+
Sets a list of spider attributes to be skipped from testing your callbacks. These attributes will still be recorded.
153
+
`Default: []`
154
+
155
+
###### Settings
156
+
157
+
-**AUTOUNIT_RECORD_SETTINGS**
158
+
Sets a list of settings names to be recorded in the generated test case.
159
+
`Default: []`
130
160
131
161
---
132
-
**Note**: Remember that you can always apply any of these settings per spider including them in your spider's `custom_settings` class attribute - see https://docs.scrapy.org/en/latest/topics/settings.html#settings-per-spider.
162
+
**Note**: Remember that you can always apply any of these settings per spider including them in your spider's `custom_settings` class attribute - see https://docs.scrapy.org/en/latest/topics/settings.html#settings-per-spider.
163
+
133
164
134
165
## Command line interface
135
166
@@ -162,20 +193,26 @@ The original request that triggered the callback.
162
193
***`response`***
163
194
The response obtained from the original request and passed to the callback.
164
195
165
-
***`result`***
196
+
***`output_data`***
166
197
The callback's output such as items and requests.
198
+
_Same as ***`result`*** prior to v0.0.28._
167
199
168
200
***`middlewares`***
169
201
The relevant middlewares to replicate when running the tests.
170
202
171
203
***`settings`***
172
204
The settings explicitly recorded by the *`AUTOUNIT_INCLUDED_SETTINGS`* setting.
173
205
174
-
***`spider_args`***
175
-
The arguments passed to the spider in the crawl.
206
+
***`init_attrs`***
207
+
The spider's attributes right after its _\_\_init\_\__ call.
208
+
209
+
***`input_attrs`***
210
+
The spider's attributes right before running the callback.
211
+
_Same as ***`spider_args`*** or ***`spider_args_in`*** prior to v0.0.28._
176
212
177
-
***`python_version`***
178
-
Indicates if the fixture was recorded in python 2 or 3.
213
+
***`output_attrs`***
214
+
The spider's attributes right after running the callback.
215
+
_Same as ***`spider_args_out`*** prior to v0.0.28._
179
216
180
217
Then for example, to inspect a fixture's specific request we can do the following:
0 commit comments