pytest-dev / pytest-flask

A set of pytest fixtures to test Flask applications

Home Page:http://pytest-flask.readthedocs.org/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Set live server host and port in .ini file

daniel-butler opened this issue · comments

Is this a direction we would like to see with this library? I can add it if that is the case

Yeah I think that would be a good addition!

How are you planning to name the options? I suggest flask_server_host and flask_server_port (or flask_live_server_* if you prefer).

flask_live_server_* makes more sense to me if we don't put it in a section.

I'm not sure how, or if it is possible, but what about a separate config section? Something like:

[flask]
live_server_host = 
live_server_port = 

What do you think?

That would be good, but it is not really possible right now with pytest's config system, so let's go with flask_live_server_* then. 👍

Ok sounds good, I’m out of my element when it comes to pytest plugins, the direction really helps!

What are some methods to setup the tests for ini configurations? When looking at how this would be possible it seems the implementation is pretty easy. Essentially add pytestconfig.getini(‘flask_live_server_*’) in the right places.

Take a look at these two tests:

@pytest.mark.parametrize('port', [5000, 5001])
def test_live_server_fixed_port(self, port, appdir):
appdir.create_test_module('''
import pytest
def test_port(live_server):
assert live_server.port == %d
''' % port)
result = appdir.runpytest('-v', '--live-server-port', str(port))
result.stdout.fnmatch_lines(['*PASSED*'])
assert result.ret == 0
@pytest.mark.parametrize('host', ['127.0.0.1', '0.0.0.0'])
def test_live_server_fixed_host(self, host, appdir):
appdir.create_test_module('''
import pytest
def test_port(live_server):
assert live_server.host == '%s'
''' % host)
result = appdir.runpytest('-v', '--live-server-host', str(host))
result.stdout.fnmatch_lines(['*PASSED*'])
assert result.ret == 0

You would do pretty much the same thing, except you would write a pytest.ini file next to the test file instead of passing host/port to pytest.

But I just realized that we already have --live-server-port and --live-server-host options, so they can already be configured in your pytest.ini:

[pytest]
addopts = --live-server-port 127.0.0.1 --live-server-port 5000

This also has the advantage that it can be overwritten in the command-line.

So in the end I think this issue is not really necessary. Sorry for not realizing sooner, I'm not actually the creator of the project but just help maintain it.

No worries about the change in direction. I don’t really understand the rational between an ini config vs a command line option. What is the thought process for why we would put something as its own ini config vs a command line option? That might be too much to explain here or if you happen to know of an article that explains it in detail that works for me!

Usually command-line options are used when users will type the options in the command-line often, while ini options are used to have reasonable defaults that will be used most of the time.

It really depends on the plugin, but this is generally how I think of them.

Well I'm closing this for now given that I don't think there's any actions left to do here, but feel free to continue to comment here.

My use case for adding it to the .ini is I'm running selenium grid to tests against the flask app's docker container. Because networking in docker has to match up exactly I was looking to always set the to 0.0.0.0 and the port to a mapped container port. Because this has to be setup always in order for the tests to run I was thinking it might make sense to put it as an ini config.

I'm currently using addopts = which is working. Just wasn't sure what message we would be relaying if we put it as a separate ini config instead of appending it to the addopts configuration.

Just wasn't sure what message we would be relaying if we put it as a separate ini config instead of appending it to the addopts configuration.

I think this is fine actually... what are your concerns?

Note that even ini config values can be overwritten in the command-line with the -o option.

Wow you are fast! My only concern was if it made sense to set them up in the ini file. I don't have much experience with configuring ini filies so I didn't even know if it was the appropriate place.

Not sure how I missed the tests for the current configs. For my own amusement, would the ini tests be setup something like this?

     @pytest.mark.parametrize('port', [5000, 5001]) 
     def test_ini_flask_live_server_fixed_port(self, port, appdir, testdir): 
         
         testdir.makeini('''
             [pytest]
             flask_live_server_port = %d
             ''' % port)
         
         appdir.create_test_module(''' 
             import pytest 
  
             def test_port(live_server): 
                 assert live_server.port == %d 
         ''' % port) 
         result = appdir.runpytest('-v') 
         result.stdout.fnmatch_lines(['*PASSED*']) 
         assert result.ret == 0

    # same thing for host..

Thanks again for the responses!

My only concern was if it made sense to set them up in the ini file. I don't have much experience with configuring ini filies so I didn't even know if it was the appropriate place.

Yeah I think so. As I said, the user can override it in the command-line if he/she wants.

For my own amusement, would the ini tests be setup something like this?

You got it. 👍

I appreciate all of the help, thanks again!