Questions tagged [fixtures]
Fixtures are scopes used for running tests which isolate each test from global namespace pollution and side-effects from previous tests in order to avoid false positives and false negatives
1,747
questions
572
votes
6
answers
251k
views
What is conftest.py for in Pytest?
I'm trying to understand what conftest.py files are meant to be used for.
In my (currently small) test suite I have one conftest.py file at the project root. I use it to define the fixtures that I ...
349
votes
6
answers
140k
views
What are fixtures in programming?
I have heard of this term many times (in the context of programming) but couldn't find any explanation of what it meant. Any good articles or explanations?
265
votes
14
answers
282k
views
How to pass a parameter to a fixture function in Pytest?
I am using py.test to test some DLL code wrapped in a python class MyTester.
For validating purpose I need to log some test data during the tests and do more processing afterwards. As I have many ...
124
votes
18
answers
56k
views
Problems with contenttypes when loading a fixture in Django
I am having trouble loading Django fixtures into my MySQL database because of contenttypes conflicts. First I tried dumping the data from only my app like this:
./manage.py dumpdata escola > ...
99
votes
4
answers
35k
views
pytest fixtures Redefining name from outer scope [pylint]
I'm learning pytest and I lint my code with pylint.
But pylint still complaints about:
W0621: Redefining name %r from outer scope (line %s)
for the following example from pytest:
# test_wallet.py
@...
94
votes
6
answers
97k
views
How to create a fixture file
I want to create a fixture file in my Django project.
How can I do this?
85
votes
1
answer
19k
views
How do I escape the ERB tag in ERB
I have a simple fixture.yml file:
label:
body: "<%= variable %>"
The issue is that the ERB code is parsed as part of loading the fixture, whereas I actually want the body to be literally "&...
71
votes
7
answers
47k
views
@Patch decorator is not compatible with pytest fixture
I have encountered something mysterious, when using patch decorator from mock package integrated with pytest fixture.
I have two modules:
-----test folder
-------func.py
-------...
70
votes
4
answers
22k
views
Django: Create fixtures without specifying a primary key?
One of the things that bugs me about Django fixtures is that you've got to specify every model's primary key. Is there any way to create fixtures without having to specify a primary key for each row?
69
votes
5
answers
47k
views
How to pass arguments to pytest fixtures?
The baseline of all my tests is that there will always be a taxi with at least one passenger in it. I can easily achieve this setup with some basic fixtures:
from blah import Passenger, Taxi
@pytest....
63
votes
5
answers
48k
views
In which order are pytest fixtures executed?
For an application I'm testing I'd like to create an autouse=True fixture which monkeypatches smtplib.SMTP.connect to fail tests if they try to send an email unexpectedly.
However, in cases where I ...
58
votes
4
answers
12k
views
How do I prevent fixtures from conflicting with django post_save signal code?
In my application, I want to create entries in certain tables when a new user signs up. For instance, I want to create a userprofile which will then reference their company and some other records for ...
57
votes
9
answers
47k
views
Loading fixtures in django unit tests
I'm trying to start writing unit tests for django and I'm having some questions about fixtures:
I made a fixture of my whole project db (not certain application) and I want to load it for each test, ...
50
votes
6
answers
33k
views
Load and use fixture in rails console
I wonder if there's a way to load and/or use fixture in rails console. Actually, I'd like to create a user from my fixture users.yml to do some testing without having to go through all the "pain" of ...
47
votes
3
answers
41k
views
How and where does py.test find fixtures
Where and how does py.test look for fixtures? I have the same code in 2 files in the same folder. When I delete conftest.py, cmdopt cannot be found running test_conf.py (also in same ...
45
votes
4
answers
11k
views
When to use pytest fixtures?
I'm new to testing and I've stumbled across pytest fixtures, but I'm not entirely sure when to use them and why they're useful.
For example, see the below code:
import pytest
@pytest.fixture
def ...
42
votes
10
answers
16k
views
Using factory_girl in Rails with associations that have unique constraints. Getting duplicate errors
I'm working with a Rails 2.2 project working to update it. I'm replacing existing fixtures with factories (using factory_girl) and have had some issues. The problem is with models that represent ...
42
votes
2
answers
27k
views
difference between fixture and yield_fixture in pytest
I am going through pytest fixtures, and the following looks pretty similar, latest works pretty similar.
Yes, the readability is better in yield_fixure, however could someone let me know what ...
40
votes
4
answers
28k
views
In Django, getting a "Error: Unable to serialize database" when trying to dump data?
I'm getting an error when I'm trying to dump data to a JSON fixture in Djanog 1.2.1 on my live server. On the live server it's running MySQL Server version 5.0.77 and I imported a lot of data to my ...
39
votes
3
answers
22k
views
Override a pytest parameterized functions name
My parameters determine the name of my parameterized pytest. I will be using a some randomized params for these tests. In order for my reporting names in junit to not get messed up, I'd like to create ...
39
votes
5
answers
14k
views
Unit testing: Is it a good practice to have assertions in setup methods?
In unit testing, the setup method is used to create the objects needed for testing.
In those setup methods, I like using assertions: I know what values I want to see in those
objects, and I like to ...
37
votes
2
answers
11k
views
Factory Girl + Mongoid embedded documents in fixtures
Let’s say you have the following mongoid documents:
class User
include Mongoid::Document
embeds_one :name
end
class UserName
include Mongoid::Document
field :first
field :...
37
votes
6
answers
14k
views
Pytest use same fixture twice in one function
For my web server, I have a login fixture that create a user and returns the headers needed to send requests. For a certain test, I need two users. How can I use the same fixture twice in one function?...
36
votes
4
answers
26k
views
pytest fixtures in a separate directory
I'm looking to create a pytest structure where I can separate the fixtures from the tests completely. The reason for this separation is that I want to include the fixtures directory as an external ...
36
votes
3
answers
44k
views
Fixtures in RSpec
I'm new to using RSpec for writing tests in a Rails application which uses a MySQL database. I have defined my fixtures and am loading them in my spec as follows:
before(:all) do
fixtures :student
...
33
votes
2
answers
10k
views
Rails fixtures -- how do you set foreign keys?
I'm reading about Rails fixtures in this guide (thanks, trevorturk). It appears you define classes in a Yaml file and they're automatically loaded into the test DB -- cool.
But if you want to specify ...
33
votes
4
answers
13k
views
How to create fixtures (for a Devise user) as a yml.erb in rails (4.1.5)?
Update 3: It seems like this is specific to fixtures in a .yml.erb - even if I have no templated code, it seems like fixtures in a yml.erb file doesn't get loaded. Having a plain .yml file works. This ...
32
votes
7
answers
14k
views
Generate Symfony2 fixtures from DB?
Is it possible to generate fixtures from an existing DB in Symfony2/Doctrine? How could I do that?
Example:
I have defined 15 entities and my symfony2 application is working. Now some people are ...
30
votes
4
answers
27k
views
How can I load fixtures from functional test in Symfony 2
My DoctrineFixturesBundle is installed and I can load fixture trough the command-line but , how can I load fixtures from my functional test ?
30
votes
3
answers
25k
views
Why cant unittest.TestCases see my py.test fixtures?
I'm trying to use py.test's fixtures with my unit tests, in conjunction with unittest. I've put several fixtures in a conftest.py file at the top level of the project (as described here), decorated ...
28
votes
11
answers
22k
views
Symfony 3.4.0 Could not find any fixture services to load
I am using Symfony 3.4.0, I try to load fixtures with:
php bin/console doctrine:fixtures:load
An error occurred while creating the data, what's wrong?
28
votes
3
answers
24k
views
Rails Fixtures not loading with rspec
So, I'm trying to learn the rspec BDD testing framework in the context of a rails project. The problem I'm having is that I can't, for the life of me, get my fixtures to load properly in rspec ...
28
votes
2
answers
7k
views
Rails fixtures vs seeds
I'm learning Ruby on Rails, and I'd like to just make sure I understand the difference between fixtures and seed data.
What I understand is that fixtures are basically test data that you run your ...
28
votes
1
answer
17k
views
Django - Foreign Keys in Fixtures
I have a fixture of "User" objects (just the default Django auth ones), and am trying to create a fixture of "Profile" objects. Each profile has a one-to-one relation to a user object, and defines ...
27
votes
4
answers
10k
views
Functional testing with Rails and Devise. What to put in my fixtures?
Hi I'm wanting to do some functional testing of my Rails 3 app that uses Devise and CanCan.
In my User model I have the users age, I want to test that a user can only visit a certain page if they are:...
26
votes
3
answers
25k
views
Is it possible to load doctrine fixtures without deleting database
I have defined some fixtures in doctrine.
When i try to run using this
php app/console doctrine:fixtures:load then it asks me to purge the database.
is it possible to load it without purging ...
26
votes
4
answers
7k
views
How do I set up my fixtures for a has_and_belongs_to_many relation?
I have the following models:
class Company < ActiveRecord::Base
has_and_belongs_to_many :regions
class Region < ActiveRecord::Base
has_many :requests
has_and_belongs_to_many :companies
...
25
votes
4
answers
14k
views
Why would a pytest factory as fixture be used over a factory function?
In the py.test docs it describes declaring factory methods as fixtures, like-so:
@pytest.fixture
def make_foo():
def __make_foo(name):
foo = Foo()
foo.name = name
return ...
23
votes
3
answers
2k
views
In Rails 3, I'm getting a NoMethodError for the recycle! method during testing
I've got a functional test that's using fixtures. I also am using fixtures in my Unit tests, but they work without flaw. When running the functional tests, I get a:
NoMethodError: undefined method '...
23
votes
1
answer
12k
views
How do Django Fixtures handle ManyToManyFields?
I'm trying to load in around 30k xml files from clinicaltrials.gov into a mySQL database, and the way I am handling multiple locations, keywords, etc. are in a separate model using ManyToManyFields.
...
23
votes
1
answer
14k
views
TypeError: missing 1 required positional argument while using pytest fixture [duplicate]
I have written my test classes in a file and I am trying to use pytest fixtures so that I don't have to create the same input data in each test functions. Below is the minimal working example.
import ...
22
votes
2
answers
5k
views
Do fixtures trigger model callbacks?
Say I have a User model with the following code in User.rb:
before_create :create_dependencies
after_create :build_inbox
And I also have a users.yml file with a bunch of user fixtures defined in it.
...
20
votes
4
answers
25k
views
How to call a fixture from another fixture in Pytest?
I have a fixture that is returning an object of certain type and I have another fixture defined in another file that basically uses the object to do other things. But I am not able to return the ...
20
votes
3
answers
7k
views
Migrating Django fixtures?
I have a Django application. I have .json fixture files containing test data, with unit tests that use the data to confirm the application is working properly. I also use South to migrate my ...
19
votes
3
answers
6k
views
Loading Rails Fixtures in a Specific Order when Testing
Is there a way to load Rails fixtures in a specific order while running tests? For example, take the following classes...
class User < ActiveRecord::Base
has_many :memberships
has_many :groups,...
19
votes
1
answer
25k
views
pytest-mock mocker in pytest fixture
I'm trying to find out why I don't seem to be able to use a mocked return value in a fixture.
With the following imports
import pytest
import uuid
pytest-mock example that works:
def test_mockers(...
19
votes
3
answers
10k
views
How to concatenate several parametrized fixtures into a new fixture in py.test?
If I have two parametrized fixtures, how can I create a single test function that is called first with the instances of one fixture and then with the instances of the other fixture?
I guess it would ...
19
votes
1
answer
10k
views
Is there way to directly reference to a pytest fixture from a simple (non test) function?
Is there a way to reference (and call into) a pytest fixture from a simple function that itself is not either a test_* function or also a fixture?
known examples that can use fixtures:
1)
def ...
18
votes
5
answers
13k
views
Automatic associations in ruby on rails fixtures
As described in this article, I am using automatic associations in fixtures. For example, if a region object has a country id, instead of doing "country_id": 1, I do "country": "USA". "USA" is a ...
18
votes
7
answers
12k
views
factory_girl + rspec doesn't seem to roll back changes after each example
Similar to the problem described here:
http://rpheath.com/posts/411-how-to-use-factory-girl-with-rspec
in Short (shorten'd code):
spec_helper:
config.use_transactional_fixtures = true
config....