Python: How you import impacts how you mock

Recently, I had a problem with monkeypatching external service based function. In a nutshell, monkeypatch is a pytest fixture that allows you to replace an object or function with your mock version. Try as I might I could not get it to work. This blog post is something I wish I had read when getting my mocks to work first time.

This is the module structure with get_user function that connects to LDAP. We want to mock get_user in our tests to avoid connecting to LDAP and to not have to use real users for tests.

base.py

def get_user(uun):
    return ca_user

SCENARIO 1: importing get_user function in the module where it’s required

forms.py

from central_auth.base import get_user

def clean_user(uun):
   ca_user = get_user(uun)

Trying to monkeypatch it as below will not have the desired effect. The real get_user still gets called.

tests.py

from central_auth import base

def test_form_POST_OK(monkeypatch):
    monkeypatch(base, 'get_user', get_mock_user_function)

    get_view_with_form() # where get_user is called

# DOES NOT WORK

This is because when we do named importation i.e. importing object or function as opposed to module, the object/function gets a new namespaced name. This way what exists as central_auth.base.get_user is referred to forms.get_user within forms.py.

To make the monkeypatch work, base module should be imported like so:

SCENARIO 2: module import

forms.py

from central_auth import base

def clean_user(uun):
   ca_user = base.get_user(uun)

Then we end up having central_auth.base.get_user and forms.base.get_user, both referring to the same base module.

Alternatively, we can use unittest.mock.patch to the same effect which also allows a greater level of granularity:

SCENARIO 1:

forms.py

from central_auth.base import get_user

def clean_user(uun):
   ca_user = get_user(uun)
tests.py

@mock.patch('forms.get_user', side_effect=get_mock_user_function)
def test_form_OK(form_get_user)

SCENARIO 2:

forms.py

from central_auth import base

def clean_user(uun):
   ca_user = base.get_user(uun)
tests.py

@mock.patch('forms.base.get_user', side_effect=get_mock_user_function)
def test_form_OK(forms_get_user)

The only disadvantage of using mock.patch is that if get_user is called in different modules in your tested function then you need to mock all of them specifically like so:

forms.py

from central_auth import base

def clean_user(uun):
   ca_user = base.get_user(uun)
views.py

from central_auth import base

def view_user(request, uun):
    ca_user = base.get_user(uun)
tests.py

@mock.patch('forms.base.get_user', side_effect=get_mock_user_function)
@mock.patch('views.base.get_user', side_effect=get_mock_user_function)
def test_form_OK(views_get_user, forms_get_user):
    get_view_with_form()

 

Since I am a newbie to python and pytest, give me a shout if I got something terribly wrong. For now, my tests work 🙂

Cheers

Accelerating End User Testing

Do you develop software?

I suspect if you are looking at blogs in this space then that’s probably true, or at least it might be an area of interest for you.

Do you carry out End User Testing?

Well again, if you develop software then in most cases you are likely to be doing so with users in mind.

So End User Testing can be a pretty difficult thing to really get to grips with. In Applications Division we have adopted a technology called TestRail to help us, and frankly, it’s really doing an impressive job

http://www.gurock.com/testrail/

TestRail allows us to do things that have previously been terrifically hard and complex to coordinate. It allows us to understand how User Testing is going and to rapidly get defects straight back into development as they surface in our testing processes. Tracking progress is done through a very intuitive gui and when defects are found they are created by the tester and JIRA is created immediately. No messing about, straight back to development. This is really impacting our productivity positively

Often with big systems there can be quite literally hundreds of workflows and user test cases that need to be validated by teams of End User Testers. Keeping track of progress, or actually more importantly lack of progress, is a project manager’s nightmare. Traditionally, teams have used things like spreadsheets, email and word of mouth to know how far testers are getting on with their test scenarios and plans. Often, people unfortunately become distracted or have their plans interrupted or perhaps they might be unexpectedly absent from work. Knowing someone has not managed to complete a set of tests is crucial in making sure that things are not disappearing down rabbit holes and so that projects can complete in time.

Getting issues straight back to development allows us to start working on the problem or defect straight away, we don’t need to wait until the test run has completed, getting the tester to create the JIRA when the defect is found really speeds things up

Surfacing this has often been a really difficult thing to do but TestRail really helps to address this. It is easy to interpret and allows the project to adapt to the current situation in a way that would not really have been possible previously

We have introduced TestRail as part of our Digital Transformation programme.

You can find out more here;

http://www.projects.ed.ac.uk/project/dti002

Currently we are using TestRail in Human Resources and Finance. We are extending its use to link into both agile and waterfall projects and  expect to adopt this across the entire range of projects we undertake in Apps

If you fancy finding out more about how we are getting on with this please do get in touch

Iain

Front-End Development Community Lightning Talks

Word cloud of front-end development words in the shape of a lightning boltLast Thursday the Front-End Development Community hosted our first Lightning Talks event. Eight speakers had five minutes each to introduce us to who they are, the work they do and the tools they work with.

We had contributors and attendees from various parts of the University including the Business School, Information Services and the Edinburgh Clinical Research Facility. Despite the wide range of different teams and systems represented, we found that we have a lot in common. Lots of University staff are using or want to use Git for version control, many are using Handlebars to display content through JavaScript, and quite a few of us are thinking about service oriented architecture.

It was great to hear from so many different voices, and to start piecing together some of the common themes that run through the University. We hope to be able to follow this up with talks diving into a bit more detail, and workshops to see how we’re putting some of our common tools into use.

Continue reading “Front-End Development Community Lightning Talks”

UCISA17 and disruptive technologies

I recently attended the UCISA annual conference and exhibition.

UCISA Logo

UCISA (Universities and Colleges Information Systems Association) runs an annual conference which is a great chance to meet with peers working in the sector hear about how others have addressed challenges and develop ideas on how we can overcome obstacles that are common to our community.

You can find out a bit more about UCISA here;

https://www.ucisa.ac.uk/

I am actually the vice chair for the infrastructure group which specialises in looking at things like Cloud computing, IT security, virtualisation and many other areas and if you want to know more about that please check this out here;

https://www.ucisa.ac.uk/groups/ig

This year’s conference started with a bang and we had a fantastic presentation from Stefan Hytforrs.

Stefan is a freelance speaker who lectures on how innovation, disruptive technologies and behavioural change affects both the world of business and of course social change. Stefan presented a fantastic example of how new games like Pokomon Go have grabbed the attention of huge numbers of people and altered their behaviour. He shows a great example of hoards of people frantically chasing a virtual pokamon in fields and from a non participant’s point of view it looks simply incredible.

However his lecture really discusses far more interesting questions about what actually we regard as success. He sees the importance of community and people as the vital component in success and believes that really this is at the heart of success.

Stefan goes on to open or eyes to the fact that for the first time in our history we are truly connected, not in a hierarchy but in a peer to peer collaboration and it is here that things really start to resonate for me when we think about the objectives of the software development community of practice.

I highly recommend taking a look at Stefan’s blog and his videos, this is really a person interested in creating a better future

Home

Hear him talking about the future here you might like it;

 

 

 

Our University’s front-end development community

Last fall, I sent around a survey to members of the University in advance of setting up our new Front-End Development Community. The survey asked for information about how people might want the community to work, and also about what they were doing in their jobs. In this post, I’ll share some of the results along with my thoughts.

The survey was sent out to various groups that either do or engage with front-end development. Not all of these groups were technical – for example, I also included some of the user groups for our University CMS platform, EdWeb. This could include everything from content authors to administrators. Overall, only 66 people responded to the survey, so we are only seeing a small slice of the University as a whole. However, it still gives an interesting picture of some of the work which people are doing. I was surprised to see the diversity among some of the responses.

Continue reading “Our University’s front-end development community”

Improving curriculum data quality with better tools

Every student in the University of Edinburgh is enrolled on a degree programme and has some sort of “degree programme table” (DPT): a set of rules which guide the individual courses they’ll take during their studies.

For some programmes, the DPT is just a selection of the courses you must take each year. Others add choices for students (“select French 1A or Arabic 1A”) or let them select from a wide range of courses (“select any level 10 courses in the Moray House School of Education”). These rules are joined together with simple and/or logic.

On Wednesday we released a new version of our DPT editor. Those familiar with the old editor will be pleased to see a big UI update, plus features to natively support core courses and unstructured degrees. More exciting to us though are changes to improve the quality of future DPTs.

Continue reading “Improving curriculum data quality with better tools”

Harry Roberts: Refactoring CSS Without Losing Your Mind

Last Thursday we were lucky enough to be able to welcome Harry Roberts from CSS Wizardry (https://csswizardry.com/) to give a talk on “Refactoring CSS Without Losing Your Mind”. Many thanks to Harry for taking the time out to speak to us on this subject. The talk was organised and funded by the University’s Front-End Development Community, a new subset of the Software Development Community.  If you aren’t part of the community yet, check out our community channel on Slack.

Slides and a video of the talk (for those that missed it), can be found here:

Overall the event was a huge success, with almost 100 people attending! Since this is one of our first community events, I thought people might be interested to learn a little bit more about who attended. The numbers come from the Events Booking application so won’t be exact – some people may have attended without booking, and others may have booked but not attended.

Continue reading “Harry Roberts: Refactoring CSS Without Losing Your Mind”

Synergistic design solutions using a dry-wipe medium

This blog post is not brought to you by Simply Glass Wipeboards, nor do I receive any form of commission on purchases or compensation for writing it.


So a while back I got tired of throwing away pad after pad of paper drawing mockups and making notes. Iain swung by my desk recently and saw my solution: a desktop glass whiteboard, which he thought might be worth sharing with everyone.

Pictured below is an early design of this blog post I drew up on it. Honest.

Whiteboard

It’s been great for to do lists: I can’t remove anything until it’s done, as it’s lost forever. It’s good for rough designs, you can quickly erase and try again – though granted not for a whole application (only being A3 sized). It’s bad for handing over designs to colleagues, as they try to steal it from you.

If of interest, this is it!
http://www.simplyglasswipeboards.co.uk/desk-top-wipe-board/desk-top-glass-dry-wipe-board-a4-size/

P.S. Eliminate all sharpie pens from near your desk if you do go for this.

Combining pdfs and images in ColdFusion using Java and iText

pdfs, images and text combined in real time

We were writing a ColdFusion application that allows users to claim expenses and add PDFs or images of receipts electronically. The client requested that a single PDF for each claim be created which included both the claim details and ALL electronic attachments.

Using <cfpdf> chewed memory and created huge files

ColdFusion has built in PDF functionality but we found that adding BOTH images and merging other pdfs on the fly had performance issues and created documents with bloated file sizes.

Using iText provided a solution

Continue reading “Combining pdfs and images in ColdFusion using Java and iText”